EXERCISES for Week 1 1. Convert 7654 from octal to hexadecimal. 2. What do the following C statements do? t = x; x = y; y = t; 3. Is the following a legal C program? #include main { int c; c = 5; printf("%d \n", c); } 4. What is wrong with the following C statement? if (a > b) then c = 0; 5. What is wrong with the following C statement? if a > b { c = 0; } 6. What is wrong with the following C statement? if (a > b) c = 0; 7. What is wrong with the following C program fragment? c = 0 b = 0; 8. What is wrong with the following C statement? if (a > b) c = 0 else b = 0; 9. What is wrong with the following C function? int square (int x); { return x*x; } 10. What value is printed out by the following program? #include int f(int x) { return x+2; } main() { int x; x = 5; printf("%d \n", f(x+2)); } Programming Exercise: Try running the following program through the compiler to see the error messages that lcc produces for semicolon errors. int square (int x); { return x*x; } main() { int a, b, c; c = 0 b = 0; if (a > b) c = 0 else b = 0; } Answers to Exercises for Week 1 1. First convert to binary: 7654 = 111 110 101 100 = 1111 1010 1100 = FAC 2. Exchanges x and y. 3. Not legal. Parentheses () needed after "main". 4. No keyword "then" in C. 5. Always need parentheses around the conditional. 6. Nothing wrong. This one is OK. 7. Missing semicolon at the end of the first line. 8. Missing semicolon before "else". 9. Extra semicolon on the first line. Only function prototypes have semicolons after the parenthesis, not function definitions. 10. Prints out "9". --------------------------------------------- READING for Week 1 Notes for lectures 1-3. Kernighan and Ritchie, 2.10 2.11 3.1 3.2 3.3 3.5 3.6