Bug 23897

Summary: gcc printf prints list of array elements in reverse order
Product: Base System Reporter: rumseyj <rumseyj>
Component: gnuAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description rumseyj 2000-12-28 00:40:01 UTC
printf("%d %d %d %d\n",a[i++],a[i++],a[i++],a[i++])
 prints array elements in reverse order

Occurs on 4.2-BETA: cc -v gives gcc version 2.95.2 19991024 (release)

Also occurs on 3.4-RELEASE: cc -v gives gcc version 2.7.2.3

How-To-Repeat: Sample program:

#include <stdio.h>

int a[] = {1,2,3,4};

int main( int argc, char **argv ) {
  int i;

  i = 0;    /* reverse order */
  printf( "%d %d %d %d\n", a[i++], a[i++], a[i++], a[i++] );

  i = 0;    /* correct order */
  printf( "%d ",  a[i++] );
  printf( "%d ",  a[i++] );
  printf( "%d ",  a[i++] );
  printf( "%d\n", a[i++] );
}
Comment 1 Jacques Vidrine freebsd_committer freebsd_triage 2000-12-28 02:02:26 UTC
State Changed
From-To: open->closed

The order of evaluation for arguments to functions is undefined 
by Standard C.   Your example is a programming error.