Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@maheshkrishnan-5rk1zA • 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-L6QVpe • 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-bM40Fx • 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-Ce3knx • 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-5rk1zA • May 16, 2007
SujathaHi 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.
<a href="https://en.wikipedia.org/wiki/Undefined_behavior" target="_blank" rel="nofollow noopener noreferrer">Undefined behavior - Wikipedia</a>
Thanks,
Mahesh Krishnan -
@dayanandavt-oxochS • 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)
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.
Ordinary compiler - It will loop 100 times accordingly then print "J".
Regards
daya