#include /**** * * This program declares a list of 10 strings, with each string up to 100 * usable chars long. It reads the strings from stdin until EOF. * * A list in a C program is stored in an array. Therefore, this program * declares an array of strings. Since strings are themselves arrays, arrays * of strings are arrays of arrays. * */ #define NUM_STRINGS 10 #define STRING_SIZE 101 int main() { /* List of 10 strings, 10 chars each. The size of 100 refers to the * number of usable chars; the last char must always be a null char, which * is why STRING_SIZE is defined to be 101. */ char string_list[NUM_STRINGS][STRING_SIZE]; int i; /* Outer array index */ int j; /* Print loop index */ int input_status; /* Return value from scanf */ /* * Prompt the user. */ printf("Enter up to 10 strings, up to 100 chars each:\n"); /* * Read the first string. */ input_status = scanf("%100s", string_list[0]); /* * Loop until 10 strings are read or EOF, whichever occurs first. */ for (i = 1; i < NUM_STRINGS && input_status != EOF; i++) { input_status = scanf("%100s", string_list[i]); } /* * Print out the string list to confirm that it's been read in OK. */ for (j=0; j < i - 1; j++) { printf("List element %d: %s\n", j+1, string_list[j]); } return 0; }