-4

In the following program

#include<stdio.h>
int main()
{
int a;
a=5; printf("%d %d %d\n", a, a++ , a++);  //statement 1
a=5; printf("%d %d %d\n", a, ++a , ++a);  //statement 2
return 0;
}

Output

7 6 5
7 7 7

My question why there is different behavior with a++ and ++a. I know in variable length argument it is executed from left to right and statement 1 make sense but I am wondering the result for statement2 and I was expecting results like 7 7 6..Am I missing something here ?

night_hawk
  • 101
  • 1
  • 6
  • 2
    This question gets asked about six times a week. Punch "sequence point preincrement postincrement" into your favorite search engine. – David Schwartz Apr 08 '15 at 23:50
  • I guess you asked, because if you search on google the browser crashed, because there were just too many dupes ? – Rizier123 Apr 08 '15 at 23:50
  • 2
    See: http://stackoverflow.com/questions/2989704/order-of-operations-for-pre-increment-and-post-increment-in-a-function-argument?rq=1 – chqrlie Apr 08 '15 at 23:51
  • 2
    *I know in variable length argument it is executed from left to right*... No you don't! **order of evaluation** of function arguments is **unspecified**, even for variable argument functions. – chqrlie Apr 08 '15 at 23:53
  • Read the C FAQ at least once: [Under my compiler, the code ...](http://c-faq.com/expr/evalorder2.html) – Sinan Ünür Apr 08 '15 at 23:53
  • You should bold "evaluation". It's important to understand that you're talking about the order in which the arguments are **evaluated** that's unspecified. – David Schwartz Apr 08 '15 at 23:54
  • Why do people keep posting sewage code? Only a total moron would ever use such crap. – Martin James Apr 09 '15 at 00:15
  • 1
    @Martin James: newbies are trying to understand increment and order of evaluation. They use trial and error: they try something, print stuff and interpret the output... Undefined behaviour, like pointers, is a difficult concept for beginners. Insulting them is as useless as complaining about the rain. – chqrlie Apr 09 '15 at 01:41
  • There should be a canonical answer somewhere explaining that you *can't* learn this stuff by trial and error. You *must* read and understand specifications. Trial and error will teach you that crossing a road without looking both ways works fine -- until you happen to be near a busy road. – David Schwartz Apr 09 '15 at 04:12

1 Answers1

-1

The order in which arguments to a function are evaluated is unspecified. It's your responsibility to write code that works the same regardless of this order.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    Is the OP's code just unspecified behaviour or plain undefined behaviour per multiple reads on `a`? – chqrlie Apr 09 '15 at 00:01
  • UB, @chqrlie... You're right. "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings." – autistic Apr 09 '15 at 01:10