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 1 of 10.
Deepak Kumar said:
5 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.
(6)
Sumath said:
5 years ago
I didn't get it properly, please explain me anyone.
(2)
TEDD said:
1 year ago
BODMAS meaning.
B = BRACKETS
O = ORDERS/ EXPONENTIAL
D = DIVISION
M = MULTIPLICATION
A = ADDITIONS
S = SUBTRACTION
B = BRACKETS
O = ORDERS/ EXPONENTIAL
D = DIVISION
M = MULTIPLICATION
A = ADDITIONS
S = SUBTRACTION
(1)
Team BI said:
9 years ago
int a=2,b=3,c;
a=(b++)+(++b)+a;
c=a>b?a:b;
b=(a++)+(b--)+a;
c=c++*b--
Find the final values of a, b and c.
a=(b++)+(++b)+a;
c=a>b?a:b;
b=(a++)+(b--)+a;
c=c++*b--
Find the final values of a, b and c.
(1)
Rajeev said:
8 years ago
Hi, I am not getting this, Please anyone explain me.
(1)
CrownedEagle said:
7 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)
Latha said:
6 years ago
How left associativity is calculated, I didn't understand? Please explain.
(1)
Sriramprashad said:
9 years 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.
Can anyone explain?
Answer:-
b= a++ + ++a ( use and change + change and use)
b= 10 + 12 = 22 ( because a=10 used and incremented by 1 to 11 , incremented by 1 and changed to 12)
a++= 12 ( use and change current value value is 12 and incremented by 1 to 13
a=13
++a = 14 (change and use) current value is 13 and incremented by 1 to 14.
Output should be ( 22,12,13,14).
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.
Can anyone explain?
Answer:-
b= a++ + ++a ( use and change + change and use)
b= 10 + 12 = 22 ( because a=10 used and incremented by 1 to 11 , incremented by 1 and changed to 12)
a++= 12 ( use and change current value value is 12 and incremented by 1 to 13
a=13
++a = 14 (change and use) current value is 13 and incremented by 1 to 14.
Output should be ( 22,12,13,14).
Kamlesh yadav said:
9 years ago
int x=3,y,z;
z=y=x;
z=y+=x=-z;
printf("%d%d%d",x,y,z);
For this code, I get the output as -3 0 0.
But, I can't understand the real execution.
Anyone can explain?
z=y=x;
z=y+=x=-z;
printf("%d%d%d",x,y,z);
For this code, I get the output as -3 0 0.
But, I can't understand the real execution.
Anyone can explain?
Priyanka said:
9 years ago
if((i==j||i+j==a+1)&&(i!=1&&i!=a)&&(j!=1&&j!=a)&&(i!=1&&j!=1))
Can anyone plz explain this logic why we use this kind of logic? I cannot understand this.
Can anyone plz explain this logic why we use this kind of logic? I cannot understand this.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers