"HELLO WORLD" EXERCISES READING ------- Programming Assignment 0 Notes for lecture 1 Kernighan and Ritchie, 1.1 1.2 1.3 SUPPLEMENTAL READING -------------------- Deitel and Deitel, Chapters 1 and 2 EXERCISES --------- 1. Is the following progam legal? #include main() { int x, y; scanf("%d %d", &x, &y); printf("%d\n", x + y); } 2. Write a program that reads in two integers and prints their sum. 3. Write a program that prints "hello world" 5 times. 4. Write a program that reads in a number and prints out "hello world" that many times. . . . . ANSWERS TO "HELLO WORLD" EXERCISES 1. It depends on what the definition of "is" is. The C standard, which was instituted in the late 1980s, requires that we say "void main()", but not everyone pays attention to that. Some say that death and destruction will ensue unless everyone uses the standard; others prefer a more laissez-faire attitude that saves programmers from typing useless jargon. The "lcc" compiler enforces all the rules to help you learn them (if you want to); you can also use the "cc" or "gcc" compilers, which may sometimes view your program differently. In this case, both "cc" and "gcc" provide the "void" by default if you don't type it. % lcc hello.c hello.c:5: warning: missing return value % gcc hello.c % cc hello.c % 2. #include int main(void) { int x, y; scanf("%d %d", &x, &y); printf("%d\n", x + y); return 0; } 3. Brute-force solution: #include int main(void) { printf("hello, world\n"); printf("hello, world\n"); printf("hello, world\n"); printf("hello, world\n"); printf("hello, world\n"); return 0; } Better solution: #include int main(void) { int i; for (i = 0; i < 5; i++) printf("hello, world\n"); return 0; } 4. #include int main(void) { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) printf("hello, world\n"); return 0; }