/* File: out_of_range.c * Purpose: Illustrate what happens when an array subscript is out of * bounds. * * Compile: gcc -g -Wall -o out_of_range out_of_range.c * Run: ./out_of_range * * Input: None * Output: Depends on the system * * Notes: * 1. The final assignment will probably cause a segmentation fault, which * may result in lost output. To avoid this, add fflush(stdout) after * the printf statements. * 2. Global int i prevents assignments in for loop from overwriting i * (at least on MacOS X). * 3. You may need to experiment with the upper bound for the loop and * the subscript for the assignment x[10] = 82. */ #include int i; int main(void) { int x[3] = {5, 10, 20}; int y = 7; int z = 9; for (i = 2; i < 8; i++) { x[i] = 2; printf("i = %d, y = %d, z = %d; ", i, y, z); } x[10] = 82; return 0; }