Character Input

You could've noticed that I showed you how to print values of variables with printf, but we never tried to enter a value of any variable from they keyboard, we always had all the input values written down – we call it "hardcoded" – right in the program code.

That's because input in C is pretty complicated. You probably heard that the function to read stuff from keyboard (or, speaking strictly, from standard input) is called scanf, but to use it properly, we'll need to learn a few more things. So we'll start from something much more basic: a character input. We'll read stuff character by character and learn a lot while we figuring it out.

Reading one character

To read one character (e.g. a letter, or a number, or whatever is there) from the standard input, we use a function called getchar(), which accepts no parameters and returns an int. The return value will either be a character code of a symbol it read, or it will be equal to a special constant EOF (which normally equals to –1).

Note that it's a little bit more complicated in the real life; if you are trying to execute code on a real computer, and not here in your web browser, you'll likely notice that your program waits until you hit Enter, and only then starts returning the characters you typed. This is normal, and we'll talk about it much later.

Let's see how standard input works!

#include <stdio.h>

int main() {
	int c1, c2, c3, c4, c5, c6, c7;

	c1 = getchar();
	c2 = getchar();
	c3 = getchar();
	c4 = getchar();
	c5 = getchar();
	c6 = getchar();
	c7 = getchar();

	printf("c1: %d\n", c1);
	printf("c2: %d\n", c2);
	printf("c3: %d\n", c3);
	printf("c4: %d\n", c4);
	printf("c5: %d\n", c5);
	printf("c6: %d\n", c6);
	printf("c7: %d\n", c7);

	return 0;
}

Of course, no one writes code like that, but it will help us understand how getchar works.

Run the program. If the input still says hello, you should've got the following sequence of codes: 104, 101, 108, 108, 111, 10, and then –1. The positive numbers are codes for letters h, e, l, l, and o – called ASCII codes, 10 is the new line character (\n), and –1, as I mentioned above, is a special constant EOF, indicating that there's nothing more left.

How can we print the actual characters, and not the codes? We could change printf lines and use %c instead of %d; while %d prints its parameters as a number, %c will print a character with the corresponding code. Alternatively, we could use a function putchar(c), which prints one character.

Let's try both:

#include <stdio.h>

int main() {
	int c1, c2, c3, c4, c5, c6, c7;

	c1 = getchar();
	c2 = getchar();
	c3 = getchar();
	c4 = getchar();
	c5 = getchar();
	c6 = getchar();
	c7 = getchar();

	printf("c1: %c\n", c1);
	printf("c2: %c\n", c2);
	printf("c3: %c\n", c3);
	printf("c4: %c\n", c4);
	printf("c5: %c\n", c5);

	putchar(c1);
	putchar(c2);
	putchar(c3);
	putchar(c4);
	putchar(c5);
	putchar(c6); /* here it will print a new line character */

	return 0;
}

Try that, and on the next page we'll see how to read and print in a while loop.