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

Vsreddy said:   10 years ago
Hi friends we will see above two program:

int main()
{
int p=10,a;
a=++p + p++;
printf("%d",a);
}

->We will o from left to right first p value is incremented it becomes 11. So right now p=11;.

->Next p value is incremented but it is post increment so in expression still value of p is 11.

So final answer is a = ++p + p++;

a = 11+11;

Answer: 22.

But after completion of expression p value is 12 remember.

MELVIN said:   1 decade ago
How is it possible?

Prakash said:   1 decade ago
Hi guys. I have doubt on gcc compiler. How many programming languages can compiled by gcc compiler.

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?

Akhila said:   1 decade ago
Explain this question? According to BODMAS it is wrong so please do explain me?

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.

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.

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.

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 ?

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?


Post your comments here:

Your comments will be displayed after verification.