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:
91 comments Page 1 of 10.
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.
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)
Sumath said:
4 years ago
I didn't get it properly, please explain me anyone.
(2)
Latha said:
4 years ago
How left associativity is calculated, I didn't understand? Please explain.
(1)
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.
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)
Vaishali said:
6 years ago
@Mauli.
For your program.
The execution like this,
1) b+=++a which is equal to b=b+ ++a i.e b=20+11=31(stored in memory b=31 and a=11)
2) ++b=32( stored in memory).
3) a++=11( a=12 stored in memory now).
4) a+=b++ which is equal to a=a+ b++ i.e a=12+ 32=44( stored in memory a=44 and b=33 Because b++ is 1st assign then store in memory).
So the output is b=33 and a=44.
For your program.
The execution like this,
1) b+=++a which is equal to b=b+ ++a i.e b=20+11=31(stored in memory b=31 and a=11)
2) ++b=32( stored in memory).
3) a++=11( a=12 stored in memory now).
4) a+=b++ which is equal to a=a+ b++ i.e a=12+ 32=44( stored in memory a=44 and b=33 Because b++ is 1st assign then store in memory).
So the output is b=33 and a=44.
Jahnavi said:
6 years ago
In don't understand the answer can anyone please explain it clearly?
Rajeev said:
6 years ago
Hi, I am not getting this, Please anyone explain me.
(1)
Medss said:
6 years ago
Int a=1 ,b=2,c=3
Switch (p)
{
Case1:a++
Case2:++b
Break;
Case 3:c--;
}
System.out.println(a+","+b+","+c);
When 1:p=1 2:p=3.
Switch (p)
{
Case1:a++
Case2:++b
Break;
Case 3:c--;
}
System.out.println(a+","+b+","+c);
When 1:p=1 2:p=3.
Mauli said:
7 years ago
#include<stdio.h>
main()
{
int a=10,b=20;
b+=++a;++b;a++;
a+=b++;
printf("a=%d \n b=%d",a,b);
}
a=44
b=33
Please explain this program.
How b=33 and a=44?
main()
{
int a=10,b=20;
b+=++a;++b;a++;
a+=b++;
printf("a=%d \n b=%d",a,b);
}
a=44
b=33
Please explain this program.
How b=33 and a=44?
Sharmin ferdoush said:
7 years ago
#include<stdio.h>
main()
{
int x=5,y=6;
x=++y;
y++=x--;
x++=6+(++y);
printf("%d%d\n",x++,y);
}
Anyone explain it, please.
main()
{
int x=5,y=6;
x=++y;
y++=x--;
x++=6+(++y);
printf("%d%d\n",x++,y);
}
Anyone explain it, please.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers