On 07/17/2011 07:14 PM, Rahul Menon wrote:
According to this expression like i++ * i++and i = i++ all have undefined behavior in C. I thought these were among those 'tricky' questions (when these expressions were combined with printf functions) asked during interview and definitely expected to be having very much defined behavior!
No.. Their behaviour is 'undefined'. The ISO C language standard specifies 'undefined behaviour' as:" behavior, upon use of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirementsNOTE Possible undeļ¬ned behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message)."Typically, you'll get different results from different compilers. But there aren't any guarantees. Even with the same compiler,
it's free to do whatever it wants for such code.In C and C++ (2003), the important concept is 'sequence points' that specifies whether such code is valid or not. In the latest revision of the C++ standard (generally called C++0x), the concept of 'sequence points' has been replaced with arguably clearer 'sequenced after' and 'sequenced before' 'relations'.
I think according to them , their answer should be probably with respect to gcc compiler.
Actually, more probably for TurboC or BorlandC compiler :)Gcc (v 4.6.0) generates an 'undefined behaviour' warning for stuff like: i = i++.
Anyway have anyone come across this thing any time during their programming days.. Please throw some light regarding this!
All the time! I'll give you some common examples of undefined behaviour that I commonly see (mostly in legacy code):
int i;
short s;
float f;
double d;
1. Signed integer overflow
2. printf("%d", f);
3. scanf("%d", &s);
4. scanf("%d", &d);
5. Writing to one member of a union and then reading from another
(surprised?, anyway, this one's a bit special)
regards, Syam --"Freedom is the only law". "Freedom Unplugged"
http://www.ilug-tvm.org You received this message because you are subscribed to the Google Groups "ilug-tvm" group. To control your subscription visit http://groups.google.co.in/group/ilug-tvm/subscribe To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For details visit the google group page: http://groups.google.com/group/ilug-tvm?hl=en
