delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/22/13:59:27

Date: Sun, 22 Jun 1997 10:57:31 -0700 (PDT)
Message-Id: <199706221757.KAA16499@adit.ap.net>
Mime-Version: 1.0
To: Edevaldo <edevaldo AT sysnetway DOT com DOT br>
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Is this the normal behavior?
Cc: djgpp AT delorie DOT com

You write:
>It is more a C question than a DJGPP related one, but ...
>	When I execute this program:

>#include <stdio.h>
>
>int main( void)
>{
>        int i;
>        for( i=0; i<4; printf( "%i %i\n", i, i++) );
>        return 0;
>};
>	I was expecting it's output to be like
>0 0
>1 1
>2 2
>3 3
>	But, it was
>1 0
>2 1
>3 2
>4 3
>	Is this the normal behavior?
No; actually neither are normal behavior. See below.
>	In C the later arguments are evaluated first? The stack?
In C, the arguments are put on the stack from right to left. (This is
specified somewhere, I think in K&R, but I can't find it just now). However,
the *order* in which they are evaluated before pushing is not defined. K&R
(1st edition) Section 16 says:

The order of evaluation of function arguments is not specified by the
language. [...] The order in which side effects take place is also unspecified.

So these two compilers happen to do it differently, which is fine. Programs
are not supposed to depend on this. Your program would probably have to say:

        /* ... */
        int i;
        for (i=0;i < 4; i++) printf("%i %i\n",i,i);
        /* ... */
to get the behavior of the first compiler portably.

Consider posting such questions to comp.lang.c instead, or at least reading
the comp.lang.c FAQ. You can FTP it from rtfm.mit.edu

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019