Instructions: This homework assignment focuses primarily on issues arising when compiling and linking C programs that consist of multiple source files. The answers to the following questions can be determined by consulting a C language reference and/or writing short C programs to test your logic. For the purpose of this assignment, we will stipulate that answers are expected to be consistent with the behavior of the gcc C compiler, used with std=c99. The given code does compile, possibly with warnings. After you have analyzed the questions and decided what answers you believe are correct, you may find it useful to write some short programs to test your logic. Submit your answers to the Curator quiz collection point Quiz2. For questions 1 through 8, consider a C program organized in two source files, containing the declarations and definitions shown below (as well as additional code that's not relevant just now): // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2 extern int A2; // 1.3 int func1(int x); // 1.4 int func1(int x) { // 1.5 // body not shown int main() { if ( A2 == 10 ) { // 1.6 int A3 = func2(); // 1.7 printf("yep: %d\n", A3); // 1.8 return 0; // file2.c int A2 = 12; // 2.1 int B2; // 2.2 int func2(); // 2.3 static int func3(); // 2.4 int func2() { // 2.4 int A2 = 10; // 2.5 static int B3 = func3(); // 2.6 return A2; // 2.7 int func3() { // 2.8 int A4 = func2(); // 2.9 A2 = A4; // 2.10 return A4; // 2.11 1. The variable A1 has: 2. The variable B1 has: 1
3. The variable A2 declared in line 2.1 has: 4. The variable A2 declared in line 2.5 has: 5. The variable B3 has: If the given code is compiled as shown below, the resulting diagnostic warning messages are as shown below (with a little editing to make the line numbers conform to the way the code is shown above): Linux> gcc -o Q1 -std=c99 -Wall file1.c file2.c file1.c: In function main : file1.c:1.7: warning: implicit declaration of function func2 [-Wimplicit-function-declaration] file1.c: At top level: file1.c:4:12: warning: B1 defined but not used [-Wunused-variable] file1.c: In function func1 : file1.c:13:1: warning: control reaches end of non-void function [-Wreturn-type] file2.c:15:5: warning: func3 defined but not used [-Wunused-function] 6. The warning for line 1.7 of file1.c is generated, but there is no error message for that line. Why is there no error message? 1) The compiler assumes that func2() is a function with a return type of int, and takes no parameters, and that is correct. 2) The compiler has generated the warning message incorrectly, and there is no problem. 3) The linker finds the actual definition of func2() and it agrees with the compiler's assumption. 4) 1 and 2 only 5) 2 and 3 only only 7. If the program, as given, was executed, what would be written to stdout? 1) Yep: 10 2) Yep: 12 3) Nothing will be written to stdout. 4) Something else will be written to stdout. 2
8. Where could a call to func3() be added to the given code, without producing a compiler or linker error message? 1) within func1() 2) within func2() 3) within func3() 4) All of the above 5) 1 and 2 only only only 8) None of these 9. A C programmer wants to complete the function below so that successive calls will return successive characters of the string until the terminator is reached: char nextelement() { char* string = "Brian Kernighan"; if ( string[pos] == '\0' ) return string[pos]; else return string[pos++]; In other words, the result of executing the following code should be to print the characters Brian Kernighan.... char ch; while ( ( ch = nextelement() )!= '\0' ) { printf("%c", ch);... The programmer must modify the given code for nextelement() (which will not compile), but she is not allowed to modify the interface of the function or make any use of file-scoped values. The key is to add a declaration of the variable pos. What declaration should she add to the function body? 1) int pos = 0; 2) unsigned int pos = 0; 3) static int pos = 0; 4) Any of those would do. 5) 1 or 2 only would do. 6) 1 or 3 only would do. 7) 2 or 3 only would do. 8) None of them would do. 10. Reconsider the previous question, under the restriction that the programmer is not allowed to modify the interface or the body of the function, but she is allowed to make use of file-scoped values. What declaration should she add at the file scope level? 1) int pos = 0; 2) unsigned int pos = 0; 3) static int pos = 0; 4) Any of those would do. 5) 1 or 2 only would do. 6) 1 or 3 only would do. 7) 2 or 3 only would do. 8) None of them would do. 3
For questions 11 and 12, consider the following small C program, which is divided into three files: // main.c #include <stdio.h> #include <stdint.h> #include <inttypes.h> #include "Puzzle.h" int main() { int32_t Sum = 0; for (int k = 0; k < 100; k++) { Sum += increment(); printf("sum is %"PRId32".\n", Sum); // Puzzle.h #ifndef PUZZLE_H #define PUZZLE_H int increment(); #endif // Puzzle.c int increment() { int puzzle = 0; return 0; return ++puzzle; 11. What value will the variable Sum hold when the loop exits? 1) 0 2) 99 3) 100 4) 101 5) 4950 6) 5050 12. Aside from the arithmetic statement in the loop, why will Sum hold that particular value? 1) increment() returns k when it's called for the k-th time, because puzzle has static storage duration 2) increment() returns 0 every time it's called, because puzzle has automatic storage duration 3) increment() returns 1 every time it's called, because puzzle has automatic storage duration 4) None of these For questions 13 through 15, suppose that the file Puzzle.c shown above is replaced by: // Puzzle.c int puzzle = 0; int increment() { return ++puzzle; 13. What value will the variable Sum hold when the loop exits? 1) 0 2) 99 3) 100 4) 101 5) 4950 6) 5050 4
14. Aside from the arithmetic statement in the loop, why will Sum hold that particular value? 1) increment() returns k when it's called for the k-th time, because puzzle has static storage duration 2) increment() returns 0 every time it's called, because puzzle has automatic storage duration 3) increment() returns 1 every time it's called, because puzzle has automatic storage duration 4) None of these 15. An objection is made to the change made in the second version of Puzzle.c on the grounds that the variable puzzle is now accessible to code in other files. Which of the following statements are true? 1) The variable puzzle in the second version of Puzzle.c could not be accessed from the file main.c, no matter what changes were made to main.c. 2) The variable puzzle in the second version of Puzzle.c could be accessed from the file main.c, if a single line of code was added to main.c. 3) We could prevent access to the variable puzzle by adding private to the declaration of puzzle. 4) 1 and 3 are true. 5) 2 and 3 are true. 6) None of the statements are true. 5