C Programming - Expressions - Discussion

Discussion Forum : Expressions - General Questions (Q.No. 1)
1.
Which of the following is the correct order of evaluation for the below expression?
z = x + y * z / 4 % 2 - 1
* / % + - =
= * / % + -
/ * % - + =
* % / - + =
Answer: Option
Explanation:
C uses left associativity for evaluating expressions to break a tie between two operators having same precedence.
Discussion:
92 comments Page 1 of 10.

Poonamchandra said:   1 decade ago
@Jayesh.

Your program:

#include<stdio.h>
int main(){
int i=5,j=5,y,x;
x=++i + ++i + ++i;
y=++j + ++j + ++j;
printf("%d %d %d %d",x,y,i,j);
return 0;
}

Will surely give the output 22 22 8 8

This is because in case of incr/decr operation performed after =(equal to) operator, all prefix incr/decr are carried out first and allot value to all variables that is the value of variable at L.H.S. of =, and then for variable used after = will be incr/decr as same no of time equal to the postfix incr/decr..

example:

void main()
{
int a=5, b;
b=a++ + ++a + ++a;
printf("%d",b);
}

In line
b=a++ + ++a + +aa;

There are two prefix increment so a's value will be incremented two times, e.i. a=7 and then a will be allotted to every a variable in that line, so that b's value will be something like this:

b=7+7+7, i.e b=21 and a=7,

But we left one postfix increment operator a++, so increment a by one
, then a=8;

At final, a=8, b=21.

Try some examples and your doubt will be surely be cleared.

Coder said:   1 decade ago
@Ablaze

The operation inside the printf() statement would be performed from Right to left associativity(as the increment operator has the associativity RTL) and the result is stored in the stack.

As the stack works on LIFO (Last In First Out)concept, the printf statement will print the result in Left to Right Associativity.

Lets say

If a = 10
b=a++ + ++a; --- Evaluated as RTL
b = 11+11 = 22
a = 12 (Results of post increment operation)

printf("%d %d %d %d",b,a++,a,++a);

As the associativity of post increment operator is RTL(Right to left). It will be operated in the same way.

Result will be stored in the stack as -

++a = 13
a= 13
a++ = 13

After this statement the value of the a has incremented to 14.

While printing - The top element of the stack would be printed first.

So ++a which has become 14.

a would have the same value as 14.

And in the stack the last value 13.

So the output in gcc compiler is 22 13 14 14.

Sonal said:   1 decade ago
What is the output of this.

b=a++ + ++a + ++a if a=10.

I got b=36.

But how please explain.

Yes shahank the correct output is 35 not 36 or 34 because expression is b=a++ + ++a + ++a if a=10.

I solve it step by step ok lets check it out.

Step1-value of a=10 in memory also then.

Step 2-now value of a++ is a post increment so first it will use the same value i.e. a=10 and then do increment in the value of "a" in memory i.e a=11 but in expression the value used is 10.

Step3-now add the value ++a.

So check that now the value of a in memory is 11 i.e. a=11, and here is perincrement so now the value will increase by 1 in memory as well as in expression i.e a=12,

Step4-now the value of a is 12 in memory but in expresson the next step is again ++a so the value get again change in expression and increse by 1 i.e. a=13 in expression as well as in memory so now add all values of "a".

b=a++ + ++a + ++a.

b=10+12+13.

b=35.

CrownedEagle said:   6 years ago
Order of evaluation will be compiler specific.
The following program gave me output b = 35..
#include<stdio.h>

int main()
{
int a = 10, b;
b = a++ + ++a + ++a;
printf("b = %d\n",b);
return 0;
}

BUT the GCC also throws a warning as follows:

gcc -Wall -o "scrapbook" "scrapbook.c" -lm (in directory: /home/crownedeagle/Downloads/CDAC C-CAT/C - K&R Solutions)
scrapbook.c: In function \'main\':
scrapbook.c:6:15: warning: operation on \'a\' may be undefined [-Wsequence-point]
b = a++ + ++a + ++a;
^
scrapbook.c:6:15: warning: operation on \'a\' may be undefined [-Wsequence-point]
Compilation finished successfully.

So the compilation of program and evaluation of 'b' will be undefined, it may give different answers on different machines and on different compiler.

My advice: Don't get stuck on this too much.
(1)

Sriramprashad said:   8 years ago
int b,a=10;
b=a++ + ++a;
printf("%d %d %d %d",b,a++,a,++a);
For this code i get the output as 22 13 13 13.
But I can't understand the real execution.
Can anyone explain?

Answer:-

b= a++ + ++a ( use and change + change and use)
b= 10 + 12 = 22 ( because a=10 used and incremented by 1 to 11 , incremented by 1 and changed to 12)
a++= 12 ( use and change current value value is 12 and incremented by 1 to 13
a=13
++a = 14 (change and use) current value is 13 and incremented by 1 to 14.

Output should be ( 22,12,13,14).

Deepak Kumar said:   4 years ago
Hi @Sumath,

Since C uses left associativity for evaluating expressions to break a tie between two operators having the same precedence.

In given equation, z = x + y * z / 4 % 2 - 1.

* and / has same precedence so 1st divide it into two parts like z= x + y * z and z= z / 4 % 2 - 1. Then, apply the BODMAS rule in these two respectively.

You will get * first then + from the 1st equation.
Then,

/, % and -from 2nd equation.

Then,
Add = sign at the last.

So, finally, you will get;
*/%+-=
as per their precedence.

Hope this helps.
(5)

Praveen said:   10 years ago
Explain once again sir!

b=a++ + ++a + ++a; if a=10.

Here answer is 34 because,

Step 1: Since expression is left associate. So 1st a++ execute
here a++ = 10, cause of post prefix then it increment by 1 and store in memory, now in memory a = 11.

Step 2: Now in expression most preference given to pre increment
so both ++a store 11+1 = 12.

Step 3: Now b = a++ + ++a + ++a = 10+12+12 = 34 execute.

Here just you calculate step 2 only what about 3rd step here last ++a value is why 12, not 13 please explain.

Bollobhai deepa said:   7 years ago
Hi @Srivastava.

Here the increment and decrement operator's are performs depending upon the type of complier as if it is in turbos then the output is 42.

a=14;
Here b=(++a)+(a++)+(--a);

The operation for increment and decrement is right to left.
So first --a will be done. Now a will becomes 13 .and a++ is 13 only and at last a will be 14 for ++a.

Here a value is 14, so 14 +14 +14=42(here we have 3 a value )and 42 is assigned to b.

Amit said:   1 decade ago
@Shashanka

Your output comes out to be 34, not 36.
When the value of a is 10:
Break your question into steps, Step1: "b=a++" the value here will be taken as 10 (Post Increment: Value of a will be incremented but a++ shows only the original value of a), Step2: "b=a++ + '++a'" the value of ++a would be 12. Step3: "b=a++ + ++a + '++a'" the value for a third time would be taken as 12 only. Sum is 34.

Vsreddy said:   1 decade ago
Hi friends we will see above two program:

int main()
{
int p=10,a;
a=++p + p++;
printf("%d",a);
}

->We will o from left to right first p value is incremented it becomes 11. So right now p=11;.

->Next p value is incremented but it is post increment so in expression still value of p is 11.

So final answer is a = ++p + p++;

a = 11+11;

Answer: 22.

But after completion of expression p value is 12 remember.


Post your comments here:

Your comments will be displayed after verification.