/* * Read positive numbers from stdin, store them in an array. Print out the * array when done. This version of the store program has a rather tricky use * of the ++ operator. On the whole, such trickery is better avoided. This * can be appreciated by comparing store-tricky.c with * store-tricky-subtle-flaw.c. These two programs look like they should be * equivalent, but the first one works, the second one doesn't always work. * See if you can spot the flaw. */ #include int main() { int i = 0; /* Array index for input. */ int j = 0; /* Array index for output. */ double a[5]; /* Array to hold numbers */ /* * Prompt and read first number. */ printf("Input positive numbers, negative to stop: "); scanf("%lf", &a[0]); /* * Loop until negative number input. */ while (a[i] > 0 && ++i < 5) { /* * Read in next number. */ scanf("%lf", &a[i]); } /* * Print out the array. */ for (; j < i; j++) { printf("a[%d] = %f\n", j, a[j]); } return 0; }