Main

Lecture3 Exercises

(On real programmers)

1. New lecture exercises repository

Clone the repository, which we will be using for lecture exercises. Note that this time the remote is located at shell.uoregon.edu uses your Duck ID (not your CIS one) and requires shell.uoregon access.

git clone yourduckid@shell.uoregon.edu:/home11/bnorris2/cis330/330exercises.git

This will clone the above repository into a 330exercises local directory. You can optionally specify a different directory name in your clone command. Then cd lecture2 and create a new directory using mkdir with a unique name (e.g., your username).

2. I/O, C Strings, pointers

Let us add some useful code to the minimal program we wrote last time. We will prompt the user for some input, then based on their input, we will select a different string to output.

Start with tiny.c which you created in the previous lecture exercises. Or, if you wish, create a new file, tiny2.c for this code. First let's accept a single character as input and if it is 'y', then print a positive response, otherwise, commiserate.

  1. #include <stdio.h>   /* Needed for printf and getchar */
  2.  
  3. int main()
  4. {
  5.     char userInput;
  6.  
  7.     printf("Are you having a nice day? (y/n)? ");
  8.  
  9.     userInput = getchar();
  10.  
  11.     if (userInput == 'y') {
  12.         printf("That's wonderful, so am I!\n");
  13.     } else {
  14.         printf("That sucks, just try again tomorrow.\n");
  15.     }
  16.  
  17.     return 0;
  18. }

In the main subroutine, declare a string variable, which in C is either an array of char elements or a pointer, char *. In the first version, we will declare the userInput variable as a

  1. #include <stdio.h>   /* Needed for printf and fgets */
  2.  
  3. int main()
  4. {
  5.     /* Maximum size of string for storing user input */
  6.     const int maxBufSize = 5;
  7.  
  8.     /* String variable to store user input */
  9.     char userInput[maxBufSize];                              
  10.  
  11.     printf("Are you having a nice day? (yes/no)? ");
  12.     fgets ( userInput, maxBufSize, stdin );          
  13.  
  14.     if (strcmp(userInput,"yes\n") == 0) {
  15.         printf("That's wonderful, so am I!\n");
  16.     } else {
  17.         printf("Oh well, just try again tomorrow.\n");
  18.     }
  19.  
  20.     return 0;
  21. }

Finally, instead of having a static array of chars, let's declare our string variable as a char pointer, or char * and manage its memory explicitly.

First, we will do an illustrative exercise in class (I will need some volunteers!).

  1. #include <stdio.h>   /* Needed for printf and fgets */
  2. #include <stdlib.h>   /* Needed for malloc */
  3.  
  4. int main()
  5. {
  6.     /* Maximum size of string for storing user input */
  7.     const int maxBufSize = 5;
  8.  
  9.     /* String variable to store user input */
  10.     char *userInput = (char *) malloc (maxBufSize * sizeof(char));
  11.  
  12.     printf("Are you having a nice day? (yes/no)? ");
  13.     fgets ( userInput, maxBufSize, stdin );    
  14.  
  15.     if ( strcmp(userInput,"yes\n" ) == 0) {
  16.         printf("That's wonderful, so am I!\n");
  17.     } else {
  18.         printf("Oh well, just try again tomorrow.\n");
  19.     }
  20.  
  21.     /* Remember to free memory that has been allocated dynamically! */
  22.     free(userInput);
  23.     return 0;
  24. }

Green Marinee theme adapted by David Gilbert, powered by PmWiki