[comp.lang.c] function call argument evaluation and global variables

schmidt@glacier.ics.uci.edu (Doug Schmidt) (08/23/89)

Hi,

  I've got a short question.  Does the ANSI C standard place any
restrictions on the evaluation of parameters to functions?  In
particular, is the following program portable in a fully conforming
ANSI C compiler (I know it `fails' on non-ANSI C compilers):

----------------------------------------
int number = 100;

int 
bar ()
{
  return number += 1000;
}

foo (a, b)
int a, b;
{
  printf ("%d, %d, %d\n", a, b, number);
}

main ()
{
  foo (number, bar ());
}
----------------------------------------

This program prints

1100, 1100, 1100

if function bar () is evaluated before global variable `number.'
On the other hand, the program prints

100, 1100, 1100

if the evaluation is reversed.

I realize that to be portable one should not really upon such
behavior; I'm just interested to know whether the standard defines the
behavior here, or whether it is `caveat programmer!'

thanks,

  Doug
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
office: (714) 856-4043     |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/23/89)

In article <21448@paris.ics.uci.edu> schmidt@glacier.ics.uci.edu (Doug Schmidt) writes:
>  foo (number, bar ());
>I'm just interested to know whether the standard defines the behavior here,

The order of evaluation of the arguments to a function is unspecified, and
a sequence point occurs before the actual function call.  In your example,
either outcome is permitted.