Worksheet: C0 | CS 2113 Software Engineering - Spring 2022

Worksheet: C0

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Questions

  1. Write a program called youshotme.c that prints out the following message:

    You shot me! Ugh. Oh. Ow. Ow.
    .
    .
    .
     .-.
    (o o)
    | O \
     \   \
      `~~~'
    

    Add the program to your repo.

  2. Compile the program above with gcc.

    • What gcc command did you use?
    • Did you have any errors? If so, note them and how you fixed them.
    • If you had no errors, introduce one (like removing a ;) and note the error message.

  3. The following program should not compile… fix it and explain why.

    int main(){
      printf("Why oh why does this program fail to compile?!?!\n");
    }
    

  4. We were expecting the following output

    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're equal :-) 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    They're not equal :-| 
    

    But it’s not working :( … Can you fix the program below to get it to work?

    int main(){
      int a = 10;
      int b = 5;
      while(a > 0){
        if(a = b){
          printf("They're equal :-) \n");
        }else{
          printf("They're not equal :-| \n");
        }
        a-=1;
      }
    }
    

    Describe what was wrong?

  5. What value(s) is/are true in C? What value(s) is/are false in C?

  6. Convert the following code snippet into a function deceleration and definition.

    int minus(int a, int b){return a-b;}
    

  7. The following three programs will not compile when trying

    gcc main.c one.c two.c
    

    Fix the issues. The programs are below:

    //funcs.h
    void printOne();
    void printTwo();
    
       //one.c
       void printOne(){
         printf("one\n");
       }
    
       //two.c
       void printTwo(){
         printf("two\n");
       }
    
       //main.c
    
       int main(){
        for(int i=0;i<10;i++){
           if(i%2){
             printOne();
           }else{
             printTwo();
           }
        }
       }
    

  8. Below are Java print statements. Write the C equivalent print statement.

    int a = 5;
    int b = 2;
    float c = 4.4;
    System.out.println("a = "+a+" b="+b+" c="+c);
    

  9. What is the differences between fopen() file mode w and w+?

  10. The C program below doesn’t write anything to a file.

    int main(){
      FILE * stream = fopen("helloworld.txt", "r");
      fprintf(stream, "Hello World!\n");
      fclose(stream);
    }
    

    Fix the program and describe the error(s).

  11. What is the output of this C program?

    int main(){
       float f = 3.14;
       int n = 10;
       printf("%d %f\n",f,n);
    }
    

    Was it what you expected or not? Try and describe the process by which the output was achieved.

  12. Consider the format directive %.3g, use man 3 printf to describe what the output would be if the input was 3.141592

  13. You’re opening a file, and you get an error!?! Provide two preferred ways to report the error to stderr, as in …

    if( fopen( /* ... */) == NULL){
      // WHAT GOES HERE?!
    }
    

  14. Fix and describe the error in the code below.

    printf("Enter a number: ");
    
    int num;
    scanf("%d", num);
        
    printf("You entered %d\n", num);
    

  15. Fix and describe the error in the code below.

    printf("Enter a number: ");
    
    int num;
    scanf("%f", &num);
        
    printf("You entered %d\n", num);