CE Home
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Notices


Advertisements
Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)
Old 14th April 2007, 12:25 PM
CE - Apprentice
 
chaudharyssv's Avatar
 
Join Date: 25th September 2006
Location: New Delhi
I'm a Crazy Computer Engineer
Posts: 12
Send a message via Yahoo to chaudharyssv Send a message via Skype™ to chaudharyssv
Lightbulb 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...
__________________
"An Engineer is one who builds his own Engine"
chaudharyssv is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)
Old 16th April 2007, 05:42 PM
CE - Apprentice
 
Join Date: 26th March 2007
I'm a Crazy Electronics Engineer
Posts: 14
Default Re: Problem: Output of this C Code

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
maheshkrishnan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 26th April 2007, 11:29 PM
CE - Apprentice
 
chaudharyssv's Avatar
 
Join Date: 25th September 2006
Location: New Delhi
I'm a Crazy Computer Engineer
Posts: 12
Send a message via Yahoo to chaudharyssv Send a message via Skype™ to chaudharyssv
Post Re: Problem: Output of this C Code

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
__________________
"An Engineer is one who builds his own Engine"
chaudharyssv is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 27th April 2007, 10:42 PM
CE - Newbie
 
Join Date: 2nd April 2007
I'm a Crazy ELECTRONICS Engineer
Posts: 7
Default Re: Problem: Output of this C Code

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
Sujatha is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 2nd May 2007, 08:39 PM
Moderator
 
MaRo's Avatar
 
Join Date: 2nd May 2007
Location: Egypt
I'm a Crazy Computer Engineer
Posts: 263
Send a message via MSN to MaRo
Default Re: Problem: Output of this C Code

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
MaRo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 17th May 2007, 01:56 PM
CE - Apprentice
 
Join Date: 26th March 2007
I'm a Crazy Electronics Engineer
Posts: 14
Default Re: Problem: Output of this C Code

Quote:
Originally Posted by Sujatha View Post
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.
http://en.wikipedia.org/wiki/Undefined_behavior

Thanks,
Mahesh Krishnan
maheshkrishnan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 18th May 2007, 11:34 AM
CE - Newbie
 
Join Date: 16th May 2007
Location: Bangalore - India
I'm a Crazy Computer Science Engineer
Posts: 7
Default Re: Problem: Output of this C Code

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
dayanandavt is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +5.5. The time now is 06:59 PM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.

Advertisements