Character I/O


This lab provides practice with character-by-character input and output within the C programming language.

Reading Individual Characters

  1. Program get-3-char.c reads three characters from the keyboard and prints them.
    1. Copy get-3-char.c to your account, and compile it, and briefly describe what it does.
    2. Run the program, entering abc on the same line (no spaces), and describe what happens.
    3. Run the program, entering a b c on the same line (with spaces between the letters), and describe what happens.
    4. Run the program, trying to enter a on one line, b on a second line, and c on a third line. What happens? In the output, the first line appears as $a$, but the second and third lines contain a single dollar sign. Why?
    5. Run the program, trying to enter ab on one line, c on a second line. Again, explain what happens and why.
    6. In the program, replace
      ch1 = getchar();
      ch2 = getchar();
      ch3 = getchar();
      scanf ("%c", &ch1);
      scanf ("%c", &ch2);
      scanf ("%c", &ch3);
      Does the program behave the same (i.e., for b–e), or is something different? Why or why not?
    7. In the previous step, replace the three scanf statements by the single statement:
      scanf ("%c%c%c", &ch1, &ch2, &ch3);
      Again, determine whether the program behaves as before or does something else, and explain what you observe.
    8. Finally, try adding a space before each %c in the scanf statement:
      scanf (" %c %c %c", &ch1, &ch2, &ch3);
      Again, try different types of input as before (b - e in the steps above). How does the program behave this time and why?

Reading One Line, Character by Character with getchar

  1. Program getchar-example.c reads and prints a line of characters from the keyboard. As you observed in Step 1, the single-character printing function putchar, like getchar, deals with a single character per call.
    1. Copy getchar-example.c to your account, compile it, and briefly describe what it does.
    2. What happens when you enter more than one letter? Why?
    3. What happens if you do not enter a letter, but simply press Enter?

      Recall that getchar gets a single character; blank space and newline characters are considered viable characters.

    4. Why do you think the line
      putchar ('\n');

      is included?

      Like getchar, putchar takes a single character. The function putchar prints a single character to the "standard" output stream called stdout.

  2. Although reading and printing one character at a time is sometimes useful by itself, a more common approach is to collect a line of characters in an array. This practice is illustrated in the getchar-line-example.c program.
    1. Copy getchar-line-example.c to your account, compile it, and briefly describe what it does.
    2. Examine the first loop. Why does putchar (a) precede a = getchar()? Why does putchar ('\n') appear after the loop? What happens if you move putchar ('\n') inside the loop?
    3. Examine the second loop. What is the purpose of the index variable? The loop condition involves both an assignment and a comparison. Explain how that works and why it is used. Why is the statement line[index]=0 placed after the loop?
    4. What happens in the output if the line line[index]=0 is deleted from after the second loop? Explain.
    5. Can the line buffer be overflowed? Amend the termination test of the while loop to fix any such problem.
  3. Write a program that does the following:
    • Declares a character array one larger than some upper size limit.
    • Reads an integer from the keyboard (likely using scanf). This number will be used to determine the number of characters read in a later step below.
    • Clears any whitespace after the number from the first line of input, perhaps using the following expression:
      while (getchar() != '\n')
    • Reads the specified number of characters from the keyboard (up to the size limit).
    • Adds a null character at the end of the array.
    • Prints the resulting string.
  4. Modify the characters as they are entered in the previous step, so that it sets each letter to the opposite case when placing it in the string. Note that <ctype.h> has functions that compare types and modify letter cases. For example:
    • appleAPPLE
    • AlPhAbEtaLpHaBeT
    • X-Rayx-rAY

Programming with getchar

  1. Write a program that prints a prompt for input and analyzes a line entered by the user.
    1. Initially, the program should print the number of characters entered.
    2. Modify your program to also print the number of uppercase and lowercase letters in your input. (Hint: You might use the <ctype.h> library.)
    3. Modify your program further to print the number of whitespace characters (e.g., tabs and spaces).

Practice with printf

  1. As you have learned previously, it is important to pay close attention to variable types, and you may have seen errors that occur due to a type mismatch in a function. Look at the following code, where each printf statement uses the wrong type for the declared variable.
    #include <stdio.h>
    main (void)
      int x = 9, y;
      double s = 13.86;
      char * word = "computer";
      char ch = word[5];
      y = (int) s;
      printf ("\tThe value of x is %lf.\n", x);
      printf ("\tThe value of y is %f.\n", y);
      printf ("\tThe value of s is %d.\n", s);
      printf ("\tThe value of word is %c.\n", word);
      printf ("\tThe value of ch is %s.\n", ch);
      printf ("\tThe value of ch is %d.\n", ch);
      return 0;
    } // main
    1. Will the code compile? Save the program to a file in your directory and check. Run the program. Do any line(s) cause the program to terminate with a segmentation fault error? Why? Think about what each of the types are and how to print those types.
    2. Fix only the line(s) that prevents the code from completing, and run the code. Explain why you get each incorrect value. How are integers, doubles, and floats treated by each incorrect printf format string? How is a character different from a string? (Hint: consider how each variable type is stored and evaluated.)
    3. Change the printf format strings so that the correct values are printed when the program is run, and check that your changes fixed the code.
    4. Look at the following two lines of code:
      printf ("\tThe value of s is %lf.\n", s);
      printf ("\tThe value of s is %f.\n", s);
      Will these two lines print the same or different output? Why?
    5. Make sure both lines are in your code, compile, and run. Did the output match your expectations? If not, why did the code behave this way?
  2. Look closely at the following code. Will this program run as the programmer intended?
     * Program to print the values of different types, with proper spacing.
     * It should print a as "7", b as "15.2594", and c as "something".
    #include <stdio.h>
    main (void)
      int a = 7;
      double b = 15.2594;
      char * c = "something";
      printf ("The value of a is %d.\n", a);
      printf ("The value of b is %.4lf.\n", b);
      printf ("The value of c is %9s.\n", c);
      return 0;
    } // main
    1. Compile and run the code. Is the output what you expected?
    2. In the statement that prints the value of b, edit the %.4lf to be %.2lf. What do you think the output will be? Compile and run the code to check.
    3. Now edit the statement that prints the value of b to %.6lf. What do you think will happen? Why? Run the code to check.
    4. Given your previous experience, what do you think will happen if you edit the format string for printing the value of c from %9s to %6s. What if you change it to %12s? Run the program with each of these values. Did your results match your expectations?

Practice with printf Spacing

As you may recall, when a number is placed between the '%' character and the variable type, the printf function allocates at least that many spaces for the information. If the information takes that much space or more, then the data are printed normally. However, if the information takes less space, the information is "right-aligned" with the allocated space, with the "extra" spaces left blank. In contrast, when a '.' character (followed by a number) is placed between the '%' and the variable type, only that much space is allocated after the decimal point. For example, if the number is 48.3557, and the print statement indicates that only one value after the decimal should be printed, the printed result will show 48.3 as the variable inserted.

  1. Modify your program from Steps 6abc so that the code prints both the number and the percentage of uppercase letters, lowercase letters, and whitespace characters. Within the format string for the printf statement, print all percentages to exactly two decimal places.