function call argument evaluation and global variables

Doug Schmidt schmidt at glacier.ics.uci.edu
Wed Aug 23 07:12:11 AEST 1989


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 at 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.



More information about the Comp.lang.c mailing list