C Programming Part 6: Compilation

Topics

  • Preprocessor
  • Compiling
  • Linking
  • GCC
  • Declaration vs definition

Class Slides

Resources

Study Questions

  1. What is the role of the preprocessor?
  2. (T/F) When you include #include <stdio.h> in your code, the definition (function body) for the printf function is included in your code.
  3. (T/F) When you compile two files using gcc file1.c file2.c, the compiler will analyze both files together to make sure you are using functions and variables from different files correctly.
  4. Why does the C language use header files?
  5. Why should you put function prototypes for your helper functions at the top of your .c files?
  6. (T/F) Global variables should be defined in header files.
  7. If you are compiling your code, and see the error: “undefined reference to ‘foo’”, what does this mean?
  8. If you are compiling your code, and see the error: “multiple definition of ‘current_state’”, what does this mean?

  9. (T/F) Will this code compile?
     #include <stdio.h>
    
     int x;
     int x;
    
     int main() {
         printf("%d\n", x);
         return 0;
     }
    
  10. (T/F) Will this code compile?
     #include <stdio.h>
    
     int x;
     int x = 3;
    
     int main() {
         printf("%d\n", x);
         return 0;
     }
    
  11. (T/F) Will this code compile?
     #include <stdio.h>
    
     int x = 3;
     int x = 3;
    
     int main() {
         printf("%d\n", x);
         return 0;
     }
    
  12. The following files are compiled and linked using the command gcc main.c foo.c. Does this result in a compiler error? Linker error?
    • main.c:
        #include <stdio.h>
      
        int main() {
            int sum = add(3);
            printf("sum: %d\n", sum);
        }
      
    • foo.c
        int add(int a, int b) {
            return a + b;
        }  
      
  13. The following files are compiled and linked using the command gcc main.c foo.c. Does this result in a compiler error? Linker error?
    • main.c:
        #include <stdio.h>
      
        int x = 3;
      
        int main() {
            printf("x: %d\n", increment(x));
        }
      
    • foo.c
        int x = 4;
      
        int increment(int x) {
            return x+1;
        }