Basic Input/Output






This section deals with input and output, commonly abbreviated as I/O. Controlling I/O is how you can interact with both the user and the rest of the computer, and as such is fundamental to C programming. We'll start with output, because it's a little bit easier.

Output

We've already seen an example of how C controls output in our "hello world" program via the printf() function. printf() is provided by the standard library stdio.h, and so you must include this library to use it. printf() take up to two arguments: the first is the string you want to print out on the screen, and the second is any variable names needed in this output. Here's an example:

main()
{
   int i = 7;

   printf("A week has %d days\n", i);
}

The strange looking part of this code is the %d. This is one of the format codes for printf() (recall that I said that the f stood for formatted). Anytime printf() sees a percent sign, it immediately looks to the arguments after the double quotes, and inserts them in place of the formatting code. The code %d is used for integer arguments. A listing of useful formatting codes is below:

Formatting Codes
Code Used for
%dintegers
%ggeneral numbers
%ffloating point numbers
%ccharacters
%sstrings
%%the percent sign itself

Multiple variables can be used with printf() as long as you include enough formatting codes in the control string; but remember that the number of codes must match the number of variables! The arguments to printf() need not be variables, either: anything that will evaluate to a value is permissable.

The printf() function lets you do even more formatting of your output as well. You can control the number of spaces allocated for the display of a value, the justification of the value in that field, or the number of decimal places of a floating point number displayed. Let's go through each of these in order. First, to control the width of the field in which a value is displayed, you simply insert the width you want between the percent sign and the code letter, as in %20d. This will make a field 20 characters long containing a right-justified integer. To force the integer to be left-justified, just insert a minus sign, as in %-20d. Finally, to control the number of decimal places displayed, use a period followed by the number of places you want, as in %.10f. This will print out a floating point number to ten decimal places. All of these extra formatting options are combinable: you can easily say %-20.10f for a left-justified 10-decimal place floating point number in a field 20 characters long.

Input

While output is handled decently in C, input is more complicated and less reliable. For now, we will learn only some very simple input functions. The simplest of these is the function getchar(). This function is provided, like all the I/O functions, in stdio.h. getchar() returns an integer (the ASCII code for the character) for reasons we'll see later, but the output can easily be converted to a character via a cast. The mechanisms for reading in other types of data are more complicated than what we've seen so far, so we'll have to content ourselves with only taking in character data for the moment. An example of getchar() is below:

main()
{
   int i;

   printf("What is the first letter of your name? ");
   i = getchar();
   printf("Oh, it's %c.\n", i);
}

Don't forget to include the parentheses after getchar()!.



Back to Basic Data Types   On to Control Structures and Loops
Back to the Outline