#include /* * sample program for loop idioms */ struct list_struct { char *name; struct list_struct *next; }; main() { int i, n, c; int array[] = {1, 2, 3}; struct list_struct *p, *list; char s[] = "hello"; char *sp; struct list_struct a = {"apple", NULL}; struct list_struct b = {"banana", NULL}; a.next = &b; /* for loop */ n = sizeof(array) / sizeof(array[0]); for (i = 0; i < n; i++) printf("%d\n", array[i]); /* list search */ list = &a; for (p = list; p != NULL; p = p->next) printf("%s\n", p->name); /* endless loop */ printf("Press Control-d\n"); for(;;) { if ((c = getchar()) == EOF) break; } printf("Press Control-d\n"); while(1) { if ((c = getchar()) == EOF) break; } /* loop getting a character */ printf("Press Control-d to exit the loop\n"); while((c = getchar()) != EOF) putchar(c); /* switch */ printf("Input any character\n"); c = getchar(); switch (c) { case 'a': ; /* drop */ /* need commnet when there is drop */ case 'b': printf("%c\n", c); break; /* need break */ default: printf("other character\n"); break; /* need break at the end of default */ } /* idiom for string copy */ sp = (char *)malloc(strlen(s) + 1); /* need +1 */ strcpy(sp, s); printf("%s\n", sp); exit(0); }