GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Feb-2008, 08:22
shyam_oec shyam_oec is offline
New Member
 
Join Date: Jan 2008
Posts: 16
shyam_oec has a little shameless behaviour in the past

#define Query


CPP / C++ / C Code:
#define pro(X) (X*X)
main()
{

int i=3,j,k;
j=pro(i++);
k=pro(++i);
printf("\n%d %d",j,k);
getch();
}

output is 9 49
can any one explain why we get second output as 49.i guessed it to be 25,but after running program i was socked!
Last edited by LuciWiz : 01-Feb-2008 at 16:24. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Feb-2008, 08:33
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: #define Query


Because it is a macro, the compiler actually replaces the code with the macro. So, what really is happening is:

CPP / C++ / C Code:
main()
{ int i=3,j,k;
  //j=pro(i++);
  j = (i++ * i++); // i get incremented twice after this statement

  //k=pro(++i);
  k = (++i * ++i); // i gets incremented twice before this statement
  
  printf("\n%d %d",j,k);
  getch();
}
Notice that i gets incremented four times, not twice.
  #3  
Old 01-Feb-2008, 08:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: #define Query


Quote:
Originally Posted by shyam_oec
can any one explain...!

If you just make the macro substitution yourself, the first statement looks like
CPP / C++ / C Code:
    j = i++ * i++;

and the second one looks like
CPP / C++ / C Code:
    k = ++i * ++i;

The program indulges in undefined behavior. Any time you have an expression with side effects, and you refer to the same variable more than once, the result is undefined behavior.

You could, perhaps, infer what a particular compiler does with the code by looking at the output, but the results are actually undefined. The order of evaluation of terms in an arithmetic expression is undefined. This is very explicit in the C Standard language specification. (So, for example, in either of the two statements, the value of the variable may be incremented twice before the individual values are multiplied. Or not.)

This means that you can't depend on any other compiler to give the same result. Not even a different version of the same compiler. Saying that you "expected" some particular result is just, well, non-productive. Undefined is undefined. Period. Full stop.

You can read more at places like this: http://c-faq.com/expr/index.html

Look, particularly at http://c-faq.com/expr/evalorder2.html

Regards,

Dave
 

Recent GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists advice request promsan C Programming Language 74 23-May-2007 08:29
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 14:03
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
C++ file I/O CronoX CPP / C++ Forum 36 09-Mar-2004 17:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.