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

Mithra said:   7 years ago
Hi,

int a=10.
Then pre-decrement perform.
We got --a=9.

Then post increment perform.
We got ++a=11.

Then add two value b=9+11.

Gaurav said:   7 years ago
int a=10;
b=--a + ++a;.

Output of b is 20 how?

Tillu said:   7 years ago
@Team Bi.

int a=2,b=3,c;
a=(b++)+(++b)+a;
c=a>b?a:b;
b=(a++)+(b--)+a;
c=c++*b--

The value of a=11, b=25, c=260.

Bagavathi said:   8 years ago
Answer of c+=(a>0&&a<=10)?++a:a/b;a=50;b=10;c=20;

Jaya said:   8 years ago
a=4;
printf("%d",a=a++);
printf("%d",a);

Why the output is always 4? Can anyone explain me?

Harpreet Singh said:   8 years ago
If a=48, b=13; find a+=b++*5/a++ +b.

Mukesh said:   1 decade ago
@atul

putchar() will print char which is equal to ascii value 5+100=105 ie i..then printf will the value of i and so on.

Kiran said:   1 decade ago
int a=10;
int b;
b=a++ + ++a + ++a;

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

OUTPUT:
34 14 15 15 14 15

Please explain?

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?


Post your comments here:

Your comments will be displayed after verification.