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 8 of 10.

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.

Rahul said:   1 decade ago
What is associative and non-associative?

Kshitiya dahale said:   1 decade ago
Depends on printf execution either left to right or right to left.

@Ablaze said:   1 decade ago
Can anyone explain this????????

#include<stdio.h>
int main() {
int b,a=10;
b=a++ + a++;
printf("%d %d %d %d",b,a++,a,++a);
return 0;
}

its op in gcc compiler is 20 13 14 14

Praveen said:   1 decade ago
if a=10 then b
b=a++ + ++a;
lets convert it into steps...
1-increment(++a=11)
2-addition(11+11)
3-assignment(b=22)
4-increment(a++=12)
so output b=22,a=12

so output of
printf("%d %d %d %d",b,a++,a,++a);

is-- 22 13 13 13..

in C printf statement is evaluated from right to left
1- ++a means preincr a=a+1 means 13
2- a=13
3- a++ post incr first assign then incr so a=13
4- b=22
so output is
22 13 13 13

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.

Haimanti said:   1 decade ago
@Sanlakshmi
a=10..as b= a++ + ++a so b will be=22 and a will be 12, so right now a=12
Now in printf function we generally find out the output from right to left so ++a =13 then a=13 ..a++=13 at this time a=14..
And b =22 we already find out .

Biti said:   1 decade ago
@shivani

int b,a=10;
b=a++ + ++a;/* a++=10, ++a=11, so now b=11(bcoz now control again refreshes and calculates for a)+11=22 */
printf("%d %d %d %d",b,a++,a,++a);/* evaluates from R to L and displays from L to R. so o/p=> 22 13 13 14 */

Shivani garg said:   1 decade ago
I m still confused in dis expression
int b,a=10;
b=a++ + ++a;
printf("%d %d %d %d",b,a++,a,++a);

Pravu said:   1 decade ago
Hi shasankha, u've used 3 integers to be printed. So output will be 22 13 14.


Post your comments here:

Your comments will be displayed after verification.