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 6 of 10.
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
#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
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.
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 .
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 */
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);
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.
Jayesh saboo said:
1 decade ago
#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;
}
Can anybody explain why output is coming 22 22 8 8 ?
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;
}
Can anybody explain why output is coming 22 22 8 8 ?
Atul said:
1 decade ago
int i=5;
do
{
putchar(i+100);
printf("%d",i--);
}
while(i);
// What is the output & explain please.
do
{
putchar(i+100);
printf("%d",i--);
}
while(i);
// What is the output & explain please.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers