C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 12:25 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 4
Thumbs up Explain working of the code

What will be the output and how?
Code:
int main()
{
	int a=2,b;
	b=++a+--a;
	printf("%d %d",a,b);
}
varadharajan is offline   Reply With Quote
Old 02-09-2010, 12:29 AM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
Undefined. And how? As per the C standard of course.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 02-09-2010, 12:31 AM   #3
Registered User
 
Join Date: Jul 2009
Posts: 4
What is undefined?
varadharajan is offline   Reply With Quote
Old 02-09-2010, 12:33 AM   #4
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
The expression:
Code:
b = ++a + --a;
See c-faq undefined behavior (Make sure you READ the page).
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 02-09-2010, 12:35 AM   #5
Registered User
 
Join Date: Jul 2009
Posts: 4
I'm a new bee. Please help me to understand the working.
varadharajan is offline   Reply With Quote
Old 02-09-2010, 12:36 AM   #6
Registered User
 
Join Date: Jul 2009
Posts: 4
Ok. Thank you for your kind reply.
varadharajan is offline   Reply With Quote
Old 02-09-2010, 12:37 AM   #7
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
You couldn't have read that page in 2 minutes.
There is no answer to spoon feed (not that I would anyway), it's undefined.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 02-09-2010, 04:46 AM   #8
Registered User
 
Join Date: Feb 2010
Posts: 6
so your preincrimenting a and then predecreminting it as well.... They make it undefined because its just bad programing behavior. And it is considered dangerous code. try this one out.
Code:
int i = 0;
int k = 0;
int j = 0;
if(++k > j || i++<k || j++ == k)
{
  printf("%d, %d, %d",i , j, k);
}
what do you think the output will be? Are teacher used this on a test and the majority of us got it wrong.
kille6525 is offline   Reply With Quote
Old 02-09-2010, 04:50 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
Quote:
Originally Posted by kille6525
what do you think the output will be?
The output will be 0, 0, 1.

Quote:
Originally Posted by kille6525
Are teacher used this on a test and the majority of us got it wrong.
That just means that the students did not know their stuff or were careless

The code is perhaps a little obfuscated, but the result of the expression is well defined.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 02-09-2010, 02:51 PM   #10
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
Quote:
Originally Posted by kille6525
They make it undefined because its just bad programming behaviour.
That's certainly not why.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
K&R2 - Piece of code not working. reqonekt C Programming 2 11-13-2009 11:07 AM
Having some real trouble with this code for a game I'm working on. Swarvy Game Programming 3 05-09-2009 08:37 PM
please help me with this code... arrays arent working. ominub C Programming 3 02-24-2009 08:49 PM
<< !! Posting Code? Read this First !! >> kermi3 Linux Programming 0 10-14-2002 01:30 PM
cygwin -> unix , my code not working properly ;( CyC|OpS C Programming 4 05-18-2002 04:08 AM


All times are GMT -6. The time now is 01:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22