| Summary: | gcc printf prints list of array elements in reverse order | ||
|---|---|---|---|
| Product: | Base System | Reporter: | rumseyj <rumseyj> |
| Component: | gnu | Assignee: | freebsd-bugs (Nobody) <bugs> |
| Status: | Closed FIXED | ||
| Severity: | Affects Only Me | ||
| Priority: | Normal | ||
| Version: | Unspecified | ||
| Hardware: | Any | ||
| OS: | Any | ||
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. |
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++] ); }