[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: [LI] j = ++i * ++i * ++i : A long reply..BEWARE



Shourya Sarcar forced the electrons to say:
> 
> 	g) Was expecting something from Binand on this, fundu self-printing
> 	    proggie and also from Shanker (csewhiz@flashmail), probably busy

Well, I had posted the reason for the behaviour exhibited, but it seems
it has not reached the list. Can the listadmin please check it out? Anyway,
here it is:

The ANSI/ISO C standard allows an expression to change its value only once
between two sequence points. Sequence points are ;, ,, &&, ||, ?:, } etc. So,
in the code snippet posted, let us say the code was something like:

printf ("Hi\n");
j = ++i * ++i * ++i;

The value of i is modified thrice between the two sequence points, one at the
end of the printf and the other at the end of the next line. Notwithstanding
the operator precedence rules and the order of evaluation rules, the compiler
is given the right by the standard to do whatever it wants in such cases. And
the three compiler/OS combinations did what they wanted in this case.

The correct way to code this would be:

j = (i + 1) * (i + 2) * (i + 3); i += 3;

Here you are accessing i three times between sequence points, but that is
allowed, since you are accessing i for solely to determine its value, and not
to change it. And when you change it, you change it only once.

Did Arun Sharma post anything on this? I would appreciate it if he posted his
comments on this problem.

Binand

-- 
#include <stdio.h>                                   | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c;     | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c";    | printing program.
int main(){printf(p,10,34,p,34,10);return 0;}        | Try it!!
--------------------------------------------------------------------
The Linux India Mailing List Archives are now available.  Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.