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 2 of 10.

Praveen said:   1 decade ago
if a=10 then b
b=a++ + ++a;
lets convert it into steps...
1-increment(++a=11)
2-addition(11+11)
3-assignment(b=22)
4-increment(a++=12)
so output b=22,a=12

so output of
printf("%d %d %d %d",b,a++,a,++a);

is-- 22 13 13 13..

in C printf statement is evaluated from right to left
1- ++a means preincr a=a+1 means 13
2- a=13
3- a++ post incr first assign then incr so a=13
4- b=22
so output is
22 13 13 13

Rakesh kumar said:   8 years ago
@Jaya.

a=4;
Printf("...d",a=a++)
Ans- here a++ is in post increment that's why it will change its value in next step but here there is no any next step that's what it will print the original value of a which is 4.
And in Printf(",,...d",a).


There is no any condition for value change so it will print its real value which is 4.
That's why both answers will be 4

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.

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.

Krishna said:   1 decade ago
First a++ a value is 10 then ++a a value is 11 that is substituted in expr to b=11+11=22 then a post increment is performed to make value of a to 12.....coming to printf compiler is right associative so first fourth value that is ++a is evaluated sets a value to 13 then a and then a++ wil be printed same value then a value is set to 14 then b gets printed.....

Bhaskar Sharma said:   1 decade ago
@shashanka

For first a++ a value will be 10, and for next ++a a value will be changed to 11, and a value will be 12 for next ++a expression. Now a value is 12. So apply this value for over all expression (i.e) 12+12+12 so b will gets value 36. a will again changed its value to 13 because in above expression there is post increment (i.e) a++. Got my point.

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;
}

ANEESH said:   8 years ago
Can you explain it?

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;
}

Shahab said:   8 years ago
I have a question what will be this program output? Can anyone tell me?


#include <stdio.h>
int main()
{
int a = 2, b= 5, c = 10;
int x = 3, y = 6;
int res1 = a * b++ - (a * ++c) % b-- + c++ * a++;
int res2 = x++ * ++y;
printf("%d\n", res1);
printf("%d\n", res2);
return 0;

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?


Post your comments here:

Your comments will be displayed after verification.