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
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 7 of 10.
Rituraj said:
1 decade ago
Yaa shashank you are right.
Raksha said:
1 decade ago
Plus and minus is taken frist preference right.
Bhaskar Sharma said:
1 decade ago
@shashanka
For first a++ a value will be 10, and for next ++a a value will be changed to 11, and a value will be 12 for next ++a expression. Now a value is 12. So apply this value for over all expression (i.e) 12+12+12 so b will gets value 36. a will again changed its value to 13 because in above expression there is post increment (i.e) a++. Got my point.
For first a++ a value will be 10, and for next ++a a value will be changed to 11, and a value will be 12 for next ++a expression. Now a value is 12. So apply this value for over all expression (i.e) 12+12+12 so b will gets value 36. a will again changed its value to 13 because in above expression there is post increment (i.e) a++. Got my point.
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.
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.
Shashanka 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.
B=a++ + ++a + ++a if a=10.
I got b=36.
But how please explain.
Rocket said:
1 decade ago
Correct answer krishna!
Krishna said:
1 decade ago
First a++ a value is 10 then ++a a value is 11 that is substituted in expr to b=11+11=22 then a post increment is performed to make value of a to 12.....coming to printf compiler is right associative so first fourth value that is ++a is evaluated sets a value to 13 then a and then a++ wil be printed same value then a value is set to 14 then b gets printed.....
SanLakshmi said:
1 decade 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..
Anyone can explain?..
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..
Anyone can explain?..
Kumar said:
2 decades ago
Hi Sivaram,
Since C uses left associativity, to break up the tie between * / in the given expression z = x + y * z / 4 % 2 - 1 chooses the * first.
Since C uses left associativity, to break up the tie between * / in the given expression z = x + y * z / 4 % 2 - 1 chooses the * first.
Sri said:
10 years ago
When I tried to compile the following exp in gcc i.e.
main()
{
int a;
a=3/2*2;
printf("%d",a);
}
It gives me the answer as 2 how to solving this?
main()
{
int a;
a=3/2*2;
printf("%d",a);
}
It gives me the answer as 2 how to solving this?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers