C Exercises

  Next >
  1. Write a complete C program that displays:
    Go Cougars!
  2. Given a file named cougars.c with source code, what is the command-line to compile it into an executable?
  3. Given the following files with source code:
    main.c
    support.c
    
    what is the command-line to compile them into an executable using the two-stage compilation?
  4. Explain to your neighbor what #include does.
  5. #include <stdio.h>
    
    #define LOWER_LIMIT  0
    #define UPPER_LIMIT  42
    
    #define valid(x) ((x) > LOWER_LIMIT && (x) < UPPER_LIMIT)
    
    int main( int argc, char* argv[]){
        int ans = 41;
        printf( "ans (before): %d\n", ans);
        if( valid( ans++ ) ){
            printf( "ans is between %d and %d\n", LOWER_LIMIT, UPPER_LIMIT);
        }
        printf( "ans (after): %d\n", ans);
        
        return 0;
    }
        
    The code above compiles. Discuss with your neighbor why it produces the the following output:
    ans (before): 41
    ans (after): 43
    Hint: Look at the output of
    gcc -E cExercises-preprocessor-simple.c
  6. Enumerate all of the C data types with your neighbor.
  7. What are possible data types for each of the following?
    1.  4567
    2. -4567
    3. 1234567
    4.  98.76
    5. -98.76
    6. 500.
    7. .8
    8. 10E5
    9. 6.02E23
    10. 1234.567E89
    11. 9E-300
  8. What will be the output of the following (if any)?
    int solution;
    
    solution++;
    
    printf("solution: %d\n", solution);
    
  9. Discuss with your neighbor what will the output be of the following (if any)?
    #include <stdio.h>
    
    int main(){
        int i = 100000;
        char c = '!';
    
        c = i; 
        printf("i: %d, c: %d\n", i, c);
    
    
        return 0;
    }
    
  10. What is the value of the following expressions:
  11. int age;
    1. age = 8
    2. -age
    3. 5 + 8
    4. 5 / 8
    5. 5 / 8.0
    6. (float) 7/3
    7. ((float) 7 ) / 3
  12. What will be the output of the following (if any)?
    int i = 100000;
    char c;
    
    c = i;
    printf("i: %d, c: %d\n", i, c);
    
  13. What will be the output of the following (if any)?
    int answer = 42;
    int* answerPtr = &answer;
    
    printf("answer's value: %d\n", answer); 
    printf("answerPtr's value (base-10): %ld\n", answerPtr); 
    printf("answerPtr's value (hexadecimal): %p\n", answerPtr); 
    
  14. Write code to sum the first 50 elements of an int array named receiptTotals.
  15. Given the following code, determine the data type for each expression:
    int x;
    int* ptr;
    
    Expression Data Type Possible Values
    x    
    ptr    
    *ptr    
    &ptr    
    &x    
  16. Diagram the elements on the stack and write the output of the following (adapted from HERE):
    #include <stdio.h>
    
    int main (){
      int numbers[5];
      int * p;
      
      p = numbers;          *p = 10;
      p++;                  *p = 20;
      p = &numbers[2];      *p = 30;
      p = numbers + 3;      *p = 40;
      p = numbers;      *(p+4) = 50;
      
      for( int n = 0; n < 5; n++){
        printf("%d, ", numbers[n]);
      }
      return 0;
    }
    
  17. Write code for each of the following and make sure that it compiles:
    1. Declare a char named response as a pointer to a char
    2. Declare ex0 as a pointer to a char
    3. Declare ex1 as a pointer to a pointer to a char
    4. Declare ex2 as an array of 10 elements
    5. Declare ex3 as an array of 10 elements that are pointers to chars
    6. Declare ex4 as a pointer to an array of 30 char elements
    7. Declare ex5 as an array of 10 pointers to arrays of 500 char elements
    8. Declare ex7 as a pointer to const int
    9. Write a function ex8 that takes a char pointer named pc, increments the address that it points to and returns that value
    10. Write a function ex9 that takes an int pointer named num, and prints out what num points to and a message saying if what num points to is odd or even
  18. Add code as directed by the comments to pointersExercise.c and execute to see values update:
    int main(){
        int number = -1;
        int* intPtr1;
        int* intPtr2;
    
        // 1) Make intPtr1 point to number
    
        // 2) Assign 3 to the variable that intPtr1 is pointing to (using intPtr1)
        
        // 3) Make intPtr1 point to a newly allocated int
    
        // 4) Assign that new int to 7
        
        // 5) Copy the address stored in intPtr1 to intPtr2
    
        // 6) Make intPtr2 point to a newly allocated int
        
        // 7) Assign that new int to 1
        
        // 8) Free up the allocated memory and set both pointers to NULL
         
        return 0;
    }