![]() |
|
#1
|
|||
|
|||
#define QueryCPP / C++ / C Code:
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
|
|||
|
|||
Re: #define QueryBecause it is a macro, the compiler actually replaces the code with the macro. So, what really is happening is:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: #define QueryQuote:
If you just make the macro substitution yourself, the first statement looks like CPP / C++ / C Code:
and the second one looks like CPP / C++ / C Code:
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 GIDBlog
Prepping for deployment by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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