JMU
Sample Questions on C Development


  1. Indicate whether each of the following statements is true or false:
    (1) _____ #ifndef is a C function.
    (2) _____ #define is a preprocesor directive.
    (3) _____ -pedantic-errors is an option/switch on the cc command that instructs it to issue an error whenever the "base standard" requires a diagnostic.
  2. Explain each line in the following makefile:
        test: test.o module1.o
          cc test.o module1.o -o test
    
        test.o: test.c module1.h
          cc -c test.c
    
        module1.o: module1.c module1.h
          cc -c module1.c
    
  3. Assuming the following program is compiled and linked properly:
    #define QUIET
    #include <stdio.h>
    
    int
    main(void)
    {
      printf("Execution begins...\n");
    
      #ifndef QUIET
      for (int i=0; i<5; i++) printf("Iteration: %d\n", i);
      #endif
    
    
      printf("Execution ends.\n");
    }
        

    what will be printed when it is executed?

  4. cc -E -P filename will just run the preprocessor on filename (without including linemarkers).
    1. Given the following files:
      constants.h
      Rubber
        
      library.h
      #include "constants.h"
      Baby Buggy
        
      library.c
      #include "constants.h"
      #include "library.h"
      Bumpers
        

      what output will be generated by:

          cc -E -P library.c
          
    2. Now, suppose the files are as follows:
      constants.h
      #ifndef CONSTANTS_H
      #define CONSTANTS_H
      Rubber
      #endif
        
      library.h
      #ifndef LIBRARY_H
      #define LIBRARY_H
      #include "constants.h"
      Baby Buggy
      #endif
        
      library.c
      #include "constants.h"
      #include "library.h"
      #define  BUMPERS Bumpers
      BUMPERS
        

      what output will be generated by:

          cc -E -P library.c
          
  5. Suppose the following code is in a file named calculator.c and that there is no file named calculator.h.
    int
    add (int a, int b)
    {
      return a+b;
    }
    
    1. Will the command cc -c calculator.c -o calculator.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    2. Will the command cc calculator.c execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
  6. Suppose the following code is in a file named calculator.c and that there is no file named calculator.h.
    int
    bump(int a)
    {
      return a + increase;
    }
    
    1. Will the command cc -c calculator.c -o calculator.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    2. Suppose the following is added to the top of the file.
      #include "calculator.h"
      
      Will the command cc -c calculator.c -o calculator.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
  7. Suppose we have the following files.
    calculator.h
    int increase;
    
    calculator.c
    #include "calculator.h"
    
    int
    bump(int a)
    {
      return a + increase;
    }
    
    gradebook.c
    #include "calculator.h"
    #include <stdio.h>
    
    int main(void)
    {
      increase = 10;
      printf("Midterms Average: %d\n", add(80, 100) / 2);
      printf("Final (Curved):   %d\n", bump(70));
    
      return 0;
    }
    
    1. Will the command cc -c calculator.c -o calculator.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    2. Will the command cc -pedantic -pedantic-errors -Wall -c gradebook.c -o gradebook.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    3. Will the command cc -Wall -c gradebook.c -o gradebook.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    4. Will the command cc gradebook.o calculator.o -o gradebook execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    5. Suppose the file calculator.h is changed so that it contains the line extern int increase;. Will the two .c files compile? Will they link? If not, why not?
  8. Given the following files: lib.h
    #ifndef LIB_H
    #define LIB_H
    
    int 
    net_price(int price, int discount);
    
    static int 
    validate(int n);
    
    #endif
    
    lib.c
    #include "lib.h"
    
    int 
    net_price(int price, int discount)
    {
      return validate(price - discount);
    }
    
    static int 
    validate(int n)
    {
      if (n < 0) return 0;
      else       return n;
    }
    
    pricer.c
    #include "lib.h"
    
    int 
    main(void)
    {
      int i, j;
    
      i = validate(-5);
      j = net_price(6, 2);
    }
    
    1. Will the command cc -pedantic -pedantic-errors -Wall -c lib.c -o lib.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    2. Will the command cc -pedantic -pedantic-errors -Wall -c pricer.c -o pricer.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    3. Will the command cc -Wall -c pricer.c -o pricer.o execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?
    4. Will the command cc pricer.o lib.o -o pricer execute without error? If not, why not (and will the error be generated by the preprocessor, compiler, or linker)? If so, what file will be produced?

Copyright 2017