-1

Firstly, this is my first question here so forgive me for any mistakes.. I came across this program below :

#include<stdio.h>
main()
{
   int i=2;
   void add();
   add(i++,--i);
   printf("/ni=%d ",i);
}
void add(int a ,int b)
{
    printf("/na=%d b=%d",a,b);
}

The output is :

 a=1 b=2
 i=2

This is also posted in the below link:

Pre increment and post increment function call

when i did a little experiment, i found out that if you make the declaration "int i;" global,then output changes as:

a=1 b=1
i=2

When i searched for explanation i found out that in function calls the arguments are always pushed in stack from right. That explains the first output but how do you explain second output?

How post incrementation and pre incrementation are evaluated in case the variable is global? Or am I interpreting it all wrong?

Community
  • 1
  • 1
shalnich
  • 3
  • 1
  • 3
    You link already explains the answer to your question. It is undefined behavior. – GWW Jul 22 '14 at 19:16
  • It's undefined behaviour both ways. – chris Jul 22 '14 at 19:16
  • 1
    possible duplicate of [Why are these constructs undefined behavior?](http://stackoverflow.com/questions/949433/why-are-these-constructs-undefined-behavior) – GWW Jul 22 '14 at 19:16
  • Please see [this](http://stackoverflow.com/questions/10845122/is-the-output-of-printf-d-d-c-c-also-undefined) – Shafik Yaghmour Jul 22 '14 at 19:16
  • Normally I would just close it but I have to think about it after this [thread on meta](http://meta.stackoverflow.com/questions/266364/why-was-my-question-marked-duplicate-citing-an-existing-similar-answer). – Shafik Yaghmour Jul 22 '14 at 19:17
  • so it has nothing to do with making variable global?is it all the pre and post increment programs have undefined behavior?i am preparing for placements and a lot of c output questions i come across are similar. – shalnich Jul 22 '14 at 19:24
  • @GWW i dint get from the link i posted that it has undefined behaviour..the question had only 1 answer...also i referred to this link : https://www.mail-archive.com/algogeeks@googlegroups.com/msg18008.html that made me think in this direction – shalnich Jul 22 '14 at 19:27
  • 2
    "in function calls the arguments are always pushed in stack from right." Whoever told you this is a bad person for lying to you, and you're gullible for having believed them. C makes no guarantee that a stack *exists*, much less that arguments are passed on it, and much less in some specific order, and less still that they would be evaluated in the order in which they are pushed onto the stack that may not exist. – Stephen Canon Jul 22 '14 at 19:33
  • @StephenCanon : Thanks loads for telling that...i believed that because i found the same explanation on 2-3 more blogs on internet...i had this bookmarked so i posted it. – shalnich Jul 22 '14 at 19:44
  • Yes, sadly there are many people who are Wrong On The Internet. – Stephen Canon Jul 22 '14 at 19:54

1 Answers1

3
add(i++,--i);

This is Undefined Behaviour: The order of the evaluation of a function call arguments is unspecified, which means that it’s unspecified whether i++ or --i is evaluated first or even interleaved evaluated.

Another way of wording it is that the variable i is modified more than once between two consecutive sequence points which is Undefined Behaviour. (the , (comma) here is a separator for function parameters and does not introduce a sequence point.)

You can find more information here:

Undefined behavior and sequence points
Why are these constructs (using ++) undefined behavior? (thanks @chris)

Community
  • 1
  • 1
bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    Might be worth mentioning that the ',' operator in function calls does not introduce sequence points – Drew McGowen Jul 22 '14 at 19:17
  • @bolov: the link you added is for c++ and not for c,and link says it is no applicable for c – shalnich Jul 22 '14 at 19:38
  • Here's a C one: http://stackoverflow.com/questions/949433/why-are-these-constructs-undefined-behavior?rq=1 – chris Jul 22 '14 at 19:46
  • @shalnic: [C99](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 6.5, para 2: "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.". The `,` in the function call does not force `i++` to be evaluated before `--i`; they are *unsequenced* relative to each other, both have a side effect on the same scalar object, hence UB. – John Bode Jul 22 '14 at 21:44