5

Possible Duplicate:
confused about printf() that contains prefix and postfix operators.

I came across a code with the following snippet,

int main()  {
    int c = 100;
    printf("\n %d \t %d \n", c, c++);
    return 0;
}

I expected the output to be 100 & 101 but I get output as

 101     100

Could anyone help me know why?

Community
  • 1
  • 1
AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27
  • I guess the code is evaluating the printf parameters in reverse order, thus the c from "c++" parameter is inserted first, then the previous c parameter is inserted, which is then already incremented. If you want to do 1 plus c for the second value, and increment c too, I guess you'll have to use printf("\n %d \t %d\n", c, c+1 ); c++; – Nick Shaw Jul 08 '11 at 10:55

4 Answers4

9

The C and C++ standards do not guarantee the order of evaluation of function parameters. Most compilers will evaluate parameters from right to left because that is the order they get pushed on the stack using the cdecl calling convention.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • Man! You have to be dang quick here on answering these "why does my program do " type questions when is undefined behavior. – David Hammen Jul 08 '11 at 10:58
  • http://en.wikipedia.org/wiki/Printf it is not undefined – ashmish2 Jul 08 '11 at 11:01
  • @ashmish2: The `` in David Hammens comment is not `printf` (which is a well-defined function) but the `c, c++` arguments. That's undefined. See the previous Q on this topic, http://stackoverflow.com/questions/3109475/confused-about-printf-that-contains-prefix-and-postfix-operators – MSalters Jul 08 '11 at 11:31
  • @asmish2: The problem isn't printf. Calling `any_function_whatsoever (c, c++)` results in undefined behavior because those commas are not sequence points. It is illegal (undefined behavior) to modify and access the same value in one sequence. – David Hammen Jul 08 '11 at 11:45
5

There is no guarantee whether c on the left, or c++ on the right, will be evaluated first.

The order of evaluation of function parameters is Unspecifeid and hence Undefined Behavior as per the standard.

As per Section 1.9 of the C++ standard:

"Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function). Where possible, this International Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the abstract machine."

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • You have a big bold "Unspecified Behavior" in your question. But that's not so useful, because as a consequence the behavior is undefined. So you should have at least a bigger bolder "Undefined behavior" in it. – Johannes Schaub - litb Jul 08 '11 at 11:06
1

If you had just used printf ("%d\n", c++) or printf ("%d\n", c) the result would have been 100 in either case. Printing both c and c++ in one function call as you did is undefined behavior.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
-3

printf works from right to left so first c++ is executed (c= 100) then after C++ executes and C=101 therefore 101 and 100 is output http://en.wikipedia.org/wiki/Printf

ashmish2
  • 2,885
  • 8
  • 40
  • 54
  • 2
    -1. This is wrong. There can be no expectation of what the result of invoking undefined behavior is. – David Hammen Jul 08 '11 at 10:56
  • its not undefinef http://en.wikipedia.org/wiki/Printf read wiki atleast.. by default printf is right aligned – ashmish2 Jul 08 '11 at 11:00
  • 2
    @ashmish2: you're confusing how printf processes its arguments, and how the arguments are "prepared" at the call site before they are handed to printf. That second part (order of evaluation of the arguments in a function call) is unspecified by the standard. – Mat Jul 08 '11 at 11:12