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:
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:
5 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)
Rajeev said:
7 years ago
Hi, I am not getting this, Please anyone explain me.
(1)
ANEESH said:
8 years ago
Please expalin me q=int(a); expression.
int main (void)
{
float a;
int q;
printf("\nInsert number\t");
scanf("%f",&a);
q=(int)a;
++q;
if((q - a) != 1)
printf("\nThe number is not an integer\n\n");
else
printf("\nThe number is an integer\n\n");
return 0;
}
int main (void)
{
float a;
int q;
printf("\nInsert number\t");
scanf("%f",&a);
q=(int)a;
++q;
if((q - a) != 1)
printf("\nThe number is not an integer\n\n");
else
printf("\nThe number is an integer\n\n");
return 0;
}
Shishir said:
8 years ago
How y=a++differs from y=++a if value of a is 5?
Kamlesh yadav said:
8 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:
8 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.
Zee khan said:
8 years ago
The above program or logical operation output is 35 because prefix first increment then print and postfix first print then increment. They use one postfix and two prefix.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers