1. It is wise then to insure that the final value of an expression is 
ascertained upfront before it is being used in a function call as an
argument. I suppose this is applicable in all cases of expressions
and not limited to pre and post increments, although, pre and post
is where there is likely to be surprises.

2. Is this the case for C alone, or all major programming languages
(C++, Java, etc) deal with the argument expressions of a function
the same way? 

3. What about the scripting languages perl, php, python, etc?

Thanks


Quoting dharmendra pandit <pandit.dharmen...@gmail.com>:

> As the C specification document specifies in section 6.5.2.2 point no 10:
> 
> The order of evaluation of the function designator, the actual arguments,
> and
> subexpressions within the actual arguments is unspecified, but there
> is a sequence point
> before the actual call.
> 
> Therefore if in any function call if two or more arguments modify same
> variable using some expression
> then the order in which the expressions will be evaluated are
> unspecified, therefore gcc warning that
> operation on 'a' may be undefined. Also there is a sequence point
> before the actual call of the function
> so all the argument expressions must finish evaluation before the
> actual function call.
> 
> Also C specification document gives the following example explaining
> the above behaviour.
> 
> 12   EXAMPLE        In the function call
>           (*pf[f1()]) (f2(), f3() + f4())
>   the functions f1, f2, f3, and f4 may be called in any order. All
> side effects have to be completed before
>   the function pointed to by pf[f1()] is called.
> 
> -Dharmendra
> 
> On Mon, Jul 20, 2009 at 9:00 PM, <i...@adari.net> wrote:
> > Hello,
> >
> > Here is a program with output in gcc (4.3.2) on pre and post increments:
> >
> > //--------------------code
> begin--------------------------------------------
> > #include <stdio.h>
> >
> > main () {
> >    int a;
> >    a=1; printf ("1. %d %d\n", ++a, a);                     // 1. 2 2
> >    a=1; printf ("2. %d %d\n", a, a++);                     // 2. 2 1
> >    a=1; printf ("3. %d %d\n", a++, a);                     // 3. 1 2
> >    a=1; printf ("4. %d %d\n", a++, ++a);                   // 4. 2 3
> >    a=1; printf ("5. %d %d\n", ++a, a++);                   // 5. 3 1
> >    a=1; printf ("6. %d %d %d\n", ++a, a, a++);             // 6. 3 3 1
> >    a=1; printf ("7. %d %d %d\n", a++, a, ++a);             // 7. 2 3 3
> >    a=1; printf ("8. %d %d %d %d\n", a, a++, ++a, a);       // 8. 3 2 3 3
> >    a=1; printf ("9. %d %d %d %d\n", a, ++a, a++, a);       // 9. 3 3 1 3
> >    a=1; printf ("10. %d %d %d %d %d\n", a, a++, a, ++a, a);// 10. 3 2 3 3
> 3
> >    a=1; printf ("11. %d %d %d %d %d\n", a, ++a, a, a++, a);// 11. 3 3 3 1
> 3
> > }
> > //--------------------code end--------------------------------------------
> >
> > The output from the program is listed next to it in comments. I thought
> > I knew something about pre and post increments, but this program busted
> > my understanding of the pre and post increments. I would appreciate
> > if someone could explain me the output.
> >
> > Thanks
> >
> >
> >
> 



Reply via email to