CSC 161 Grinnell College Fall, 2013
Imperative Problem Solving and Data Structures

Laboratory Exercise on More Input and Output


The goal of this lab is to continue to practice the concepts of and functions concerning input and output with the C programming language.

Lab Exercises

Remember that for exercises using strings, you need to include the following lines in the head of your code:

#include <stdio.h>
#include <string.h>

Introduction to scanf

  1. After reviewing scanf-example.c, write a short program that asks for your name, stores your name using scanf, then prints the word "hello" and your name.


    • scanf recognizes format %d for reading an integer
    • scanf recognizes format %f for reading a float
    • scanf recognizes format %lf for reading a long float or double
    • scanf recognizes format %c for reading a character
    • scanf recognizes format %s for reading a string
  2. Run the scanf-example.c program again, except this time enter two numbers (one for the initial number and one for the word). Does the program still work? Why?

    Hint: what type does scanf assign input to in your code?

  3. Next, run the scanf-example.c program, using a phrase as input (e.g. "down the hill"). What is the result?

    Recall that scanf assigns input to a variable; when assigning input to a string, a blank space is not considered to be part of a string by scanf. This is in contrast to getchar, which treats blank space characters the same as alpha-numeric or punctuation characters.

  4. Run the scanf-example.c program one more time — this time entering two names (one for the initial number and one for the word). Does the program still work? Why?

    Hint: again, consider what types scanf is assigning.

  5. Why might someone use getchar instead of scanf?
  6. Run the program one more time.

    • if you are using a 32-bit computer, enter the following data:

      • 0123456789012345678901234567890123456
      • 0123456789012345678901234567890123456
    • if you are using a 64-bit computer, enter the following data:

      • 01234567890123456789012345678901234567890123456
      • 01234567890123456789012345678901234567890123456

    What output is generated? Can you guess why? Note that the string has 37 characters.

Comparing Strings

  1. Some programs perform different actions based on the entered information. For instance, programs that change a password often require the user to enter the new password twice to guard against typos. If the input is not the same for both entries, the password is not changed. Write a short program that prompts the user to enter a word, then prompts the user to retype the word. If the input matches, the program should print out the line "The entered word was (word)" (parentheses not included). If the input does not match, the program should print out a line which includes both entries.

    Hint: the <string.h> function strcmp (char * str1, char * str2); compares two strings, and

    • returns a negative number if str1 comes before str2 in dictionary order (upper/lower case matters)
      Examples: strcmp("cat", "dog") < 0
      strcmp("that", "this") < 0
    • returns 0 if the strings match.
      Examples: strcmp("cat", "cat") == 0
      but strcmp("cat", "cAt") ≠ 0 (lower case a and upper case A are different)
    • returns a positive number if str1 comes after str2 in dictionary order (upper/lower case matters)
      Examples: strcmp("dog", "cat") > 0
      strcmp("this", "that") > 0
    1. Test whether the program works as intended by entering words which match, and words which do not match.

    2. Enter words which are identical, except for capitalization (for example, "apple" versus "apPle"). What happens? Why do you get this result?

    3. Modify your program so that it makes all the letters in a word the same case, and rerun your tests.

    Real World Note: The above program is a simplified version of a normal password-checking function used to safeguard data. In real-world applications, a database stores the valid password(s) after the passwords have been "hashed". Hashing takes input, puts the input through an algorithm that returns the data in a way that is nearly impossible to extract the original data from, and stores it. Then, when a user enters their password in the program, their password as it is entered is hashed and compared with the stored hashed password. If the hash results are the same, the user may access the data protected by the password. One of the main reasons to store the hashed password instead of the plain text of the password is security; even if a hacker were to gain access to the password list, they would not know the actual passwords to access the secure data.

Converting Strings

  1. In exercise 2, you found out that entering a number which is assigned to a string yields the number, but treated as a string. For example, if you entered 123, the string result is "123". Write a program that reads in an integer as a char * variable, and converts the number to an integer.

    You may not use the function atoi (char * str); in your program. atoi is a built-in function that takes in a string and returns it as an int.

Robot I/O

  1. Write a short program that connects to the robot, asks for beep length and pitch, beeps for the assigned length and pitch, and disconnects from the robot.

  2. Modify the program you wrote in the previous exercise so the program continues, prompting for input and beeping, until the time entered is 0.

  3. Now modify the program you have written to count the number of beeps, and when the time entered is 0, print the number of beeps before exiting the program.

Reviewing I/O

  1. Write a short program that takes in a string and prints it out, using scanf and printf.
    1. Modify your program so that it uses putchar to print the string.
    2. Now modify the program to use getchar to take in the string.
    3. Give one reason a programmer may choose to use each version.

Feedback Welcome

Development of laboratory exercises is an interactive process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.