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 3 of 10.
Rahul said:
1 decade ago
What is associative and non-associative?
Coder said:
1 decade ago
@Ablaze
The operation inside the printf() statement would be performed from Right to left associativity(as the increment operator has the associativity RTL) and the result is stored in the stack.
As the stack works on LIFO (Last In First Out)concept, the printf statement will print the result in Left to Right Associativity.
Lets say
If a = 10
b=a++ + ++a; --- Evaluated as RTL
b = 11+11 = 22
a = 12 (Results of post increment operation)
printf("%d %d %d %d",b,a++,a,++a);
As the associativity of post increment operator is RTL(Right to left). It will be operated in the same way.
Result will be stored in the stack as -
++a = 13
a= 13
a++ = 13
After this statement the value of the a has incremented to 14.
While printing - The top element of the stack would be printed first.
So ++a which has become 14.
a would have the same value as 14.
And in the stack the last value 13.
So the output in gcc compiler is 22 13 14 14.
The operation inside the printf() statement would be performed from Right to left associativity(as the increment operator has the associativity RTL) and the result is stored in the stack.
As the stack works on LIFO (Last In First Out)concept, the printf statement will print the result in Left to Right Associativity.
Lets say
If a = 10
b=a++ + ++a; --- Evaluated as RTL
b = 11+11 = 22
a = 12 (Results of post increment operation)
printf("%d %d %d %d",b,a++,a,++a);
As the associativity of post increment operator is RTL(Right to left). It will be operated in the same way.
Result will be stored in the stack as -
++a = 13
a= 13
a++ = 13
After this statement the value of the a has incremented to 14.
While printing - The top element of the stack would be printed first.
So ++a which has become 14.
a would have the same value as 14.
And in the stack the last value 13.
So the output in gcc compiler is 22 13 14 14.
Kiran said:
1 decade ago
int a=10;
int b;
b=a++ + ++a + ++a;
printf("%d %d %d %d %d %d",b,a++,a,++a,a--,++a);
OUTPUT:
34 14 15 15 14 15
Please explain?
int b;
b=a++ + ++a + ++a;
printf("%d %d %d %d %d %d",b,a++,a,++a,a--,++a);
OUTPUT:
34 14 15 15 14 15
Please explain?
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 ?
Poonamchandra said:
1 decade ago
@Jayesh.
Your program:
#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;
}
Will surely give the output 22 22 8 8
This is because in case of incr/decr operation performed after =(equal to) operator, all prefix incr/decr are carried out first and allot value to all variables that is the value of variable at L.H.S. of =, and then for variable used after = will be incr/decr as same no of time equal to the postfix incr/decr..
example:
void main()
{
int a=5, b;
b=a++ + ++a + ++a;
printf("%d",b);
}
In line
b=a++ + ++a + +aa;
There are two prefix increment so a's value will be incremented two times, e.i. a=7 and then a will be allotted to every a variable in that line, so that b's value will be something like this:
b=7+7+7, i.e b=21 and a=7,
But we left one postfix increment operator a++, so increment a by one
, then a=8;
At final, a=8, b=21.
Try some examples and your doubt will be surely be cleared.
Your program:
#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;
}
Will surely give the output 22 22 8 8
This is because in case of incr/decr operation performed after =(equal to) operator, all prefix incr/decr are carried out first and allot value to all variables that is the value of variable at L.H.S. of =, and then for variable used after = will be incr/decr as same no of time equal to the postfix incr/decr..
example:
void main()
{
int a=5, b;
b=a++ + ++a + ++a;
printf("%d",b);
}
In line
b=a++ + ++a + +aa;
There are two prefix increment so a's value will be incremented two times, e.i. a=7 and then a will be allotted to every a variable in that line, so that b's value will be something like this:
b=7+7+7, i.e b=21 and a=7,
But we left one postfix increment operator a++, so increment a by one
, then a=8;
At final, a=8, b=21.
Try some examples and your doubt will be surely be cleared.
Siva said:
1 decade ago
@Shashanka.
a = 10;
Step1:
a++ = 11; (now a=11);
Step2:
++a = 11+1=12; (now a=12);
Step3:
++a = 12+1=13;
So the result is 11+12+13=36;
And then finally you got 36.
a = 10;
Step1:
a++ = 11; (now a=11);
Step2:
++a = 11+1=12; (now a=12);
Step3:
++a = 12+1=13;
So the result is 11+12+13=36;
And then finally you got 36.
Anil patel said:
1 decade ago
@SONAL.
b=a++ + ++a + ++a; if a=10.
HERE ANS IS 34 BECAUSE,
Step 1 - Since expression is left associate. So 1st a++ execute
here a++ =10, cause of post prefix then it increment by 1 and store in memory, now in memory a=11.
Step 2 - Now in expression most preference given to pre increment
so both ++a store 11+1=12.
Step3 - Now b = a++ + ++a + ++a = 10+12+12 = 34 execute.
b=a++ + ++a + ++a; if a=10.
HERE ANS IS 34 BECAUSE,
Step 1 - Since expression is left associate. So 1st a++ execute
here a++ =10, cause of post prefix then it increment by 1 and store in memory, now in memory a=11.
Step 2 - Now in expression most preference given to pre increment
so both ++a store 11+1=12.
Step3 - Now b = a++ + ++a + ++a = 10+12+12 = 34 execute.
Akhila said:
1 decade ago
Explain this question? According to BODMAS it is wrong so please do explain me?
Suresh said:
1 decade ago
int main()
{
int p = 10, a;
a = ++p + p++ ;
printf("%d\n",a);
}
OUTPUT = 23.
int main()
{
int p = 10, a;
a = p++ + p++;
printf("%d\n",a);
return 0;
}
OUTPUT = 21.
Can someone please explain difference between these two programs?
{
int p = 10, a;
a = ++p + p++ ;
printf("%d\n",a);
}
OUTPUT = 23.
int main()
{
int p = 10, a;
a = p++ + p++;
printf("%d\n",a);
return 0;
}
OUTPUT = 21.
Can someone please explain difference between these two programs?
Prakash said:
1 decade ago
Hi guys. I have doubt on gcc compiler. How many programming languages can compiled by gcc compiler.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers