This question already has answers here:
Order of operations for pre-increment and post-increment in a function argument? [duplicate]
(4 answers)
Closed 7 years ago.
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 ?
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.
Related
This question already has answers here:
Why the following function is called thrice
(6 answers)
Closed 5 years ago.
After executing i am getting the output as 12 6 11. please explain how this is possible
#include<stdio.h>
#define MAN(x,y) (x)>(y)?(x):(y)
int main()
{
int i = 10,j = 5,k = 0;
k = MAN(i++,++j);
printf("%d %d %d", i, j, k);
return 0;
}
The macro will expand the line
k=MAN(i++,j++)
as
k=(i++)>(j++)?(i++):(j++);
k=(i++)>(j++)?(i++), (j++) will not be evaluated.
so
i will be incremented twice and result in 12 j will be incremented once so 6 when k will be assigned i value is 11 so its value as11`.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
int a = 10;
printf("%d %d %d",++a,a++,a--);
return 0;
}
I edited the code a bit..now the outputs are : 11 9 10
It's more complex now..
It's up to the compiler in which order he evaluates the parameters of a function call.
If the compiler goes from left to right (that would explain your output):
a is 10
prä-increment which means a is incremented (the value 11 is passed as parameter)
post-decrement which means a is decremented later (the the value 11 is passed as parameter)
post-increment which means a is incremented later (the value 10 is passed as parameter)
But if I compile this e.g. with another compiler I could get different output.
Rewriting it as follows may make it easier to understand:
NOTE: I have made the assumption that the compiler will produce code to evaluate the parameters from left to right! This may be compiler specific.
#include<stdio.h>
int main()
{
int a = 10;
int param2, param3, param4;
param2 = ++a; // increments a to 11 and evaluates to 11
param3 = a--; // evaluates to current value of a then decrements a (11)
param4 = a++; // evaluates to current value of a then increments a (10)
printf("%d %d %d",param2,param3,param4);
return 0;
}
The place of increment(++) and decrement(--) operator is very important. So in case of ++a the value is incremented from 10 to 11 and then printed, for a-- the current value is printed i.e. 10 and then the a is incremented to 11. Similarly in last case a++ current value 11 is printed and it is incremented to 12.
This question already has answers here:
multiple assignment statements in printf in c [duplicate]
(2 answers)
Closed 8 years ago.
Suppose i have the following code:
#include <stdio.h>
main()
{
int a,b,c;
b=1;
c=2;
printf("%d\n",10,b=20,b=30,c=50,c=100);
printf("%d\n",b);
printf("%d\n",c);
}
o/p-10,20,50
how did the value of b became 20,not 30 ..and also the same for c?
The order of evaluation of argument expressions and their pushing on the stack are different things.
The order of evaluation of argument expressions are unspecified in C. So it might be that at first b = 20 will be evaluated and then b = 30 or vice versa.
The order of placing arguments in the stack is the following: the right most argument is placed first.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
For the testing code as follow:
#include <stdio.h>
int addTen(int x, int b[])
{
b[2] = x + b[2];
return b[2];
}
void main(void)
{
int a[3] = {4,5,6};
int i = 2;
printf("%i %i %i \n", a[i], addTen(10,a), a[i]);
}
Why is the output is 16, 16, 6? I know that even if the compiler processes the order from right to left like a[i] <- addTen(10,a) <-a[i]. After calling addTen(10,a), a[i] is already 16 (not 6). So why the output is not 16, 16,16? THANKS!
It's undefined behavior, you should read about sequence points. You're modifying a and simultaneously reading it in a same expression.
In addition the order of evaluating is not defined.
There's no order defined for evaluating arguments. The compiler is free to evaluate arguments in any order, and will normally choose the most convenient order. So, you can't define any expected output.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I compile the following code
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
It gives 27 , 6
But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?
Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.