Evaluation of arguments in function calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh V
    New Member
    • Dec 2007
    • 16

    Evaluation of arguments in function calls

    Link to the question

    20. main()
    {
    int i=5;
    printf("%d%d%d% d%d%d",i++,i--,++i,--i,i);
    }

    The output is given as 45545. But I am getting 45555. Please clarify.

    In general how to resolve these kind of problems?
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    Originally posted by Rajesh V
    Link to the question

    20. main()
    {
    int i=5;
    printf("%d%d%d% d%d%d",i++,i--,++i,--i,i);
    }

    The output is given as 45545. But I am getting 45555. Please clarify.

    In general how to resolve these kind of problems?
    Nice problem.

    I just realized compiling your example that parameters are read from right to left.

    Anyway, I see logical the output 45545:

    Take 5;

    From right to left:

    i : passes 5 to printf

    --i: Substracts 1, and then prints. passes 4 to printf

    ++i: Adds 1, and then prints. passes 5 to printf

    i--: passes 5 to printf, and then substracts one. We get 4

    i++: passes 4 to printf, and then adds one. We get 5

    Isn't it?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by jorba101
      Isn't it?
      No it isn't; according to the Standard modifying a modifiable lvalue (such as
      variable 'i') more than once before a sequence point has been reached causes
      undefined behaviour and anything can happen.

      kind regards,

      Jos

      Comment

      • jorba101
        New Member
        • Jun 2008
        • 86

        #4
        Does anybody know if is it reported / documented in GCC the described behavior?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by jorba101
          Does anybody know if is it reported / documented in GCC the described behavior?
          There is no need to document that explicitly because the GCC compiler suite is
          C99 conforming so that same piece of code produces undefined behaviour; just
          as the Standard dictates. Of course a compiler produces 'something' but you
          can't rely on it that another compiler will produce the same results.

          kind regards,

          Jos

          Comment

          • jorba101
            New Member
            • Jun 2008
            • 86

            #6
            Of course a compiler produces 'something' but you
            That's what I mean. I'm sure that GCC may produce C99-compliant code. And I'm not so sure that you can't configure it to do different things from the standard if you need too.

            What I meant is that it is likely that a particular implementation like GCC produces an output which is following some kind of internal and reproduceable rule, which is also likely to be documented somewhere. Even though you can't rely another compiler will do different. But anyway, if documented, you can rely on what GCC will do.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by jorba101
              That's what I mean. I'm sure that GCC may produce C99-compliant code. And I'm not so sure that you can't configure it to do different things from the standard if you need too.

              What I meant is that it is likely that a particular implementation like GCC produces an output which is following some kind of internal and reproduceable rule, which is also likely to be documented somewhere. Even though you can't rely another compiler will do different. But anyway, if documented, you can rely on what GCC will do.
              That would be just stupid; never ever rely on what one particular compiler does.
              If you ever need/want to port that code to another platform you're toast.

              kind regards,

              Jos

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                In general all the problems on the page linked to produce undefined behaviour in some manor. I would recommend not trying to work through them.

                Comment

                • jorba101
                  New Member
                  • Jun 2008
                  • 86

                  #9
                  Hey,

                  That's ok. I wasn't gonna put that on production stuff.

                  I'm sure there's much more scaring things yet in the flashes of our TV's, DVD players or cars...

                  :D

                  Comment

                  • jorba101
                    New Member
                    • Jun 2008
                    • 86

                    #10
                    Anyway, following the topic on parameter evaluation order.

                    As per K&R2, section 2.12, parameter order evaluation is "unspecifie d" (in C99, in C89, or in both?, is C99 an ANSI?), so operators like "++i" should be better avoided if order is important.

                    Regarding modifying several lvalues at once in a statement, ok, i promise I won't do it (why couldn't just the compiler complain, to avoid these discussions?).


                    Btw, about GCC and C99 compliance:

                    http://gcc.gnu.org/gcc-4.3/c99status.html

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by jorba101
                      Regarding modifying several lvalues at once in a statement, ok, i promise I won't do it (why couldn't just the compiler complain, to avoid these discussions?).
                      Because the compiler can't always know or see it; ex:

                      [code=C]
                      /* in file A.c */
                      int foo(int* x) { return ++(*x); }

                      /* in file B.c */
                      extern int foo(int* x);

                      int main() {

                      int i= 42;
                      printf("i= %d\n, i+++foo(&i));
                      return 0;
                      }
                      [/code]

                      kind regards,

                      Jos

                      Comment

                      • Atos
                        New Member
                        • Jun 2008
                        • 9

                        #12
                        I don't think that C99 is a standard in the complete sense...
                        Most compilers ( if not all ), do not support it.

                        Anyway, this shuold not be the case however, cause you are talking for a non-portable code using a standard that was made to produce portable code!

                        As far as i know, function arguments are not guaranteed a standard order of evaluation.

                        Comment

                        Working...