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
* / % + - =
= * / % + -
/ * % - + =
* % / - + =
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.

Deepa said:   7 years ago
Hi @Diwakar.

Your answer is incorrect. x and y values are 24.

The increment and decrement operators are performing right to left. So at first ++i became 6, ++i is 7and ++i is 8. At last, a assign to 8. So there 3 'a's. So it will be 8+8+8=24 is assigned to b.

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 */

Sriramprashad said:   8 years ago
Evaluate the following expression according to hierarchical rule, so the order of the evaluation and find the resultant data type of the expression.

int x= 2, y=30;
long l=10000;
double d=8.500;
x++ - (y-- /10) * ++x%y +l -(l+d,x--,++y) +!(d);.

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 .

Neju said:   10 years ago
int main(){
int b,a=10;
b=a++ + ++a;
printf("%d%d%d%d",b,a++,a,++a);
}

1. b = 10+12 = 22 (since a++ is 10 (11 in memory) ++a will be 12).

2. a++ 12 in memory 13.

3. a 13.

4. ++a 14.

The output 22 12 13 14. Why its not like this?

Amala said:   9 years ago
C evaluates printf() statement from right to left.if we want to make it evaluates from left to write then we have declare the function to be PASCAL.

int b,a=10;
b=a++ + ++a;
printf("%d %d %d %d",b,a++,a,++a);

Output:
22, 13, 13

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 ?

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.

Maninderpreet said:   8 years ago
#include<stdlib.h>
#include<stdio.h>
void main()
{
int a=10, i;
a=i++ + i++ + ++i + ++i;
printf("%d",a);
}

Output: 40

Can anyone help in understanding the code?

Diwakar said:   7 years 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;
}

Explain me, how x&y came as 22 instead of 21?


Post your comments here:

Your comments will be displayed after verification.