Laboratory Exercise on Character-by-Character I/O
Goals
This lab provides practice with character-by-character input and output within the C programming language.
Work Started in Class
Reading Individual Characters
-
Copy program option-prog-1.c to your account, then compile the program.
-
Run the program with the following input
i 7 i 12 r 3.5 r 2.1 q
Review the program's output and explain how this output is produced.
-
Run the program again, adding spaces, tabs, and/or new lines between the initial letter and subsequent number. Again, explain how the program produces its output.
-
Run the program one more time, adding spaces before the opening 'i', 'r', and/or 'q'. Explain what happens.
-
-
Repeat Step 1 with the revised program option-prog-2.c. What similarities or differences appear for each data set.
-
Program get-3-char.c reads three characters from the keyboard and prints them.
-
Copy get-3-char.c to your account, and compile it, and descibe briefly what it does.
-
Run the program, entering abc on the same line (no spaces), and describe what happens.
-
Run the program, entering a b c on the same line (with spaces between the letters), and describe what happens.
-
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?
-
Run the program, trying to enter ab on one line, c on a second line. Again, explain what happens and why.
-
In the program, replace
ch1 = getchar(); ch2 = getchar(); ch3 = getchar();
by
scanf ("%c", &ch1); scanf ("%c", &ch2); scanf ("%c", &ch3);
Does the program behave the same, or is something different? Why or why not?
-
In the previous step, replace the three format strings "%c" by adding a space before the %c to obtain " %c". Again, does the program behave the same, or is something different? Why or why not?
-
In the previous step, replace the three scanf statements by the single statement:
scanf ("%c%c%c", &ch1, &ch2, &ch3);
Again, determine if the program behaves as before or does something else, and explain what you observe.
-
-
Copy program scanf-s-test-1.c to your account, then compile the program.
-
Run the program, entering a string of 8 a's, and explain what happens
-
Run the program again, entering several spaces before the string of 8 a's. Again, explain what happens.
-
Run the program, entering a string of 30 or more a's. Describe what happens and hypothesize a possible cause.
-
Run the program, entering a string of 30 or more a's, followed by 30 or more b's. Again describe what happends and hypothesize a possible cause.
-
-
Change the format string in scanf-s-test-1.c to "%10s" and repeat Step 3.
-
Copy program scanf-s-test-2.c to your account, then compile and run the program. Explain the output as much as you can.
Reading One Line, Character by Character with getchar
-
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.
-
Copy getchar-example.c to your account, compile it, and descibe briefly what it does.
-
What happens when you enter more than one letter? Why?
-
How about 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.
-
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 stdout.
-
-
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 is illustrated in the getchar-line-example.c program.
-
Copy getchar-line-example.c to your account, compile it, and descibe briefly what it does.
-
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?
-
Examine the second loop. What is the purpose of the i 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[i]=0 placed after the loop?
-
What happens in the output if the line line[i]=0 is deleted from after the second loop? Explain.
-
-
Write a program that does the following:
- Reads a number from the keyboard (likely using scanf).
-
Clears any whitespace after the number from the first line of input,
perhaps using the line
while (getchar() != '\n');
- declares an array one larger than the size typed by the user on the first line
- reads the specified number of characters from the keyboard
- adds a null character at the end of the array
- prints the resulting string.
-
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.
Examples:
- apple -> APPLE
- AlPhAbEt -> aLpHaBeT
- X-Ray -> x-rAY
Homework
Programming with getchar
-
Write a program that prints a prompt for input and analyzes a line entered by the user.
-
Initially, the program should print the number of characters entered.
-
Modify your program to also print the number of uppercase and lowercase letters in your input.
Hint: You might use the <ctype.h> library.
- Modify your progrram further to print the number of whitespace characters (e.g., tabs and spaces).
-
Practice with printf
-
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> int main() { 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; }
-
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.
-
Fix 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 other? How is a character different from a string?
Hint: consider how each variable type is stored and evaluated.
-
Change the printf statements so the correct values are printed when the program is run, and check that your changes fixed the code.
-
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?
-
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?
-
-
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> int main() { 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; }
-
Compile and run the code. Is the output what you expected?
-
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.
-
Now edit the statement that prints the value of b to %.6lf. What do you think will happen? Why? Run the code to check.
-
Given your previous experience, what do you think will happen if you edit the line for the value of c from %9s to %6s. What about editing it to %12s? Run the program. Did your results match your expectations?
-
created 1 August 2011 by April O'Neill revised 10 August 2011 by April O'Neill revised 28 October 2011 by Dilan Ustek reference to integer-rep.c added 9 October 2013 by Henry M. Walker reworked and reformatted 8-9 February 2014 by Henry M. Walker readings added 19 September 2014 by Henry M. Walker printf for %.1lf clarified 9 February 2015 by Henry M. Walker expanded and reformatted 2 June 2016 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |