View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12829 Members
Join this group to post and comment.
chaudharyssv
chaudharyssv • Apr 14, 2007

Problem: Output of this C Code

main()
{
int a = 1;
int c,d;
c = ++a + ++a + ++a;
printf("\n%d",a);
printf("\n%d ", c);
getch();
}

The above C code after the execution gives the values of 'a' and 'c' as '4' and '12' resp.

Can anyone explain, why it is so...
maheshkrishnan
maheshkrishnan • Apr 16, 2007
Chaudary...

The result what you told (ie: c= 12 ) will be diffrent for diffrent compilers,
But at the ,most of the compilers the "a" in your programe will be "4" only due to the value of a which increments and then assign.
This type of codes are called, "UNDEFINED BEHAVIOUR" which will makes problem in programing.
Take care of this if you are using a 32 bit compiler for this programe.you will get the out put of "c" as "10" (Amazing isnt)
So this is an Undefined behaviour code.

Thanks,
Mahesh Krishnan
chaudharyssv
chaudharyssv • Apr 26, 2007
Hi Mahesh,

Thanks for your response.
Can you tell me more about the Undefined Behaviour? Does UB means that there is no such way in 'C' to evaluate such kind of expressions?

Waiting for your reply.

Regards,

Satendra Chaudhary
Sujatha
Sujatha • Apr 27, 2007
Hi Mahesh,

Whatever u have explained is correct. I have executed the same program in LINUX and i ot the answer as '4 ' and '10 'for 'a' and 'c' respectively.Yes its behaviour is compiler dependent.

Hi Chowdary!!!!!!!!

I think ur from AP. Its nice to see in this forum. Looking forward to share more knowledge.



Cheers
Sujatha
MaRo
MaRo • May 2, 2007
It's not weird if answers 4 & 12.

Because each ++a increment & then assign the new value to 'a', i.e ++a makes a=2 assign this value to 'a' then the second ++a increment starting from a=2 NOT a=1 & so on with the third ++a, all ++a's increment in the same time with the new value of a. So, when a=1, ++a + ++a + ++a = 2 + 2 + 2. & so on when a=3 the three ++a increment parallel to 4 make it 4 + 4 + 4=12.
Of course a=4.
Resulting a,c equals 4 & 12 respectively.

It's weird when c=10 😒

Sometimes I get c=10 & c=12 😕
maheshkrishnan
maheshkrishnan • May 17, 2007
Sujatha
Hi Mahesh,

Whatever u have explained is correct. I have executed the same program in LINUX and i ot the answer as '4 ' and '10 'for 'a' and 'c' respectively.Yes its behaviour is compiler dependent.

Hi Chowdary!!!!!!!!

I think ur from AP. Its nice to see in this forum. Looking forward to share more knowledge.



Cheers
Sujatha


Hi Choudhary , Sujatha.

Acutally Undefined behaviour can be seen not only in C, also in all compiler dependent languages.A normal example is that if you wrote a progrm. in Turbo C which you are taking an input (int i), the maximum value will be 35K' but if you run the same programme in Linux or in 32 bit compiler you can take an maximum value of 65K .
This is what at the time of real time programming you wants to take care,
Please check the link and get more on this.
https://en.wikipedia.org/wiki/Undefined_behavior

Thanks,
Mahesh Krishnan
dayanandavt
dayanandavt • May 17, 2007
Hi all,

Entire process will be depends on the compiler, If the compiler is optimised then it will print c=10 otherwise it may lead to the parallel execution like c=12.

for eg,
int j = 0;
for(i=0;i<100;i++)
j++;

print(j)


[​IMG] Optimised compiler - wont loop up to 100 times instead it learns, 100times i need to increment the j by 1. then finally print "J" without iterating the loop even a single time.

[​IMG] Ordinary compiler - It will loop 100 times accordingly then print "J".

Regards
daya

Share this content on your social channels -