How to Use Character Input/Output in C Programming

By Dan Gookin

The simplest type of input and output in C programming takes place at the character level: One character goes in; one character comes out. Of course, getting to that point involves a wee bit of programming.

Input and output devices in C programming

The C language was born with the Unix operating system. As such, it follows many of the rules for that operating system with regard to input and output. Those rules are pretty solid:

  • Input comes from the standard input device, stdin.

  • Output is generated by the standard output device, stdout.

On a computer, the standard input device, stdin, is the keyboard. Input can also be redirected by the operating system, so it can come from another device, like a modem, or it can also come from a file.

The standard output device, stdout, is the display. Output can be redirected so that it goes to another device, such as a printer or into a file.

C language functions that deal with input and output access the stdin and stdout devices. They do not directly read from the keyboard or output to the screen. Well, unless you code your program to do so.

Bottom line: Although your programs can get input from the keyboard and send output to the screen, you need to think about C language I/O in terms of stdin and stdout devices instead. If you forget that, you can get into trouble.

How to fetch characters with getchar()

It’s time for your code to become more interactive. Consider the source code from It Eats Characters, which uses the getchar() function. This function gets (reads) a character from standard input.

IT EATS CHARACTERS

#include <stdio.h>
int main()
{
  int c;
  printf("I’m waiting for a character: ");
  c = getchar();
  printf("I waited for the '%c' character.n",c);
  return(0);
}

The code in It Eats Characters reads a character from standard input by using the getchar() function at Line 8. The character is returned from getchar() and stored in the c integer variable.

Line 9 displays the character stored in c. The printf() function uses the %c placeholder to display single characters.

Exercise 1: Type the source code for project ex0701 , as shown in It Eats Characters. Build and run.

The getchar() function is defined this way:

#include <stdio.h>
int getchar(void);

The function has no arguments, so the parentheses are always empty; the word void in this example basically says so. And the getchar() function requires the stdio.h header file to be included with your source code.

getchar() returns an integer value, not a char variable. The compiler warns you when you forget. And don’t worry: The int contains a character value, which is just how it works.

Exercise 2: Edit Line 9 in the source code from It Eats Characters so that the %d placeholder is used instead of %c. Build and run.

The value that’s displayed when you run the solution to Exercise 1 is the character’s ASCII code value. The %d displays that value instead of a character value because internally the computer treats all information as values. Only when information is displayed as a character does it look like text.

Technically, getchar() is not a function. It’s a macro — a shortcut based on another function, as defined in the stdio.h header file. The real function to get characters from standard input is getc(); specifically, when used like this:

c = getc(stdin);

In this example, getc() reads from the standard input device, stdin, which is defined in the stdio.h header file. The function returns an integer value, which is stored in variable c.

Exercise 3: Rewrite the source code for It Eats Characters, replacing the getchar() statement with the getc() example just shown.

Exercise 4: Write a program that prompts for three characters; for example:

I’m waiting for three characters:

Code three consecutive getchar() functions to read the characters. Format the result like this:

The three characters are 'a', 'b', and 'c'

where these characters — a, b, and c — would be replaced by the program’s input.

The program you create in Exercise 4 waits for three characters. The Enter key is a character, so if you type A, Enter, B, Enter, the three characters are A, the Enter key character, and then B. That’s valid input, but what you probably want to type is something like ABC or PIE or LOL and then press the Enter key.

Standard input is stream-oriented. Don’t expect your C programs to be interactive. Exercise 4 is an example of how stream input works; the Enter key doesn’t end stream input; it merely rides along in the stream, like any other character.

How to use the putchar() function

The evil twin of the getchar() function is the putchar() function. It serves the purpose of kicking a single character out to standard output. Here’s the format:

#include <stdio.h>
int putchar(int c);

To make putchar() work, you send it a character, placing a literal character in the parentheses, as in

putchar('v');

Or you can place the ASCII code value (an integer) for the character in the parentheses. The function returns the value of the character that’s written.

PUTTING PUTCHAR() TO WORK

#include <stdio.h>
int main()
{
  int ch;
  printf("Press Enter: ");
  getchar();
  ch = 'H';
  putchar(ch);
  ch = 'i';
  putchar(ch);
  putchar('!');
  return(0);
}

This chunk of code uses the getchar() function to pause the program. The statement in Line 8 waits for input. The input that’s received isn’t stored; it doesn’t need to be. The compiler doesn’t complain if you don’t keep the value returned from the getchar() function (or from any function).

In Lines 9 through 12, the putchar() function displays the value of variable ch one character at a time.

Single-character values are assigned to the ch variable in Lines 9 and 11. The assignment works just like assigning values, though single characters are specified, enclosed in single quotes. This process still works, even though ch isn’t a char variable type.

In Line 13, putchar() displays a constant value directly. Again, the character must be enclosed in single quotes.

Exercise 5: Create a new project, ex0705, using the source code shown in Putting putchar() to Work. Build and run the program.

One weird thing about the output is that the final character isn’t followed by a newline. That output can look awkward on a text display, so:

Exercise 6: Modify the source code from Exercise 5 so that one additional character is output after the ! character (the newline character).