Formatted IO
The acronym "IO" stands for Input Output. "Formatted IO" refers to either arranging the structure of text for output or preparing to receive information as input. We'll be outputing information from our programs to the terminal window screen and inputing information into our program from the keyboard.
Syntax
The stdio
Library
Writing code to control information moving to and from other devices is far beyond the scope of this course. Luckily, someone else has done it for us! We can access this code through a library of code and functions. To use this library in our programs, we need to include a preprocessor directive above the main function:
#include <stdio.h>
This tells the compiler to look in the library when it encounters certain functions. Forgetting it will lead to a compiler error:
It might look intimidating, but I've outlined in red the helpful part of the error messages. Once the correct library is included, you can use the
printf
function for output and the scanf
function for input.
Vocabulary
The format string starts with the first double quote inside the parenthesis and ends with the second one. The function uses what's inside the pair of double quotes to know how things should be input or output.
A conversion specifier is like a placeholder within the format string. It holds the place of whatever variable (or value) you want to replace it during runtime. Those variables are listed after the format string, separated by commas, and correspond conversion specifiers by position (first conversion specifier matches with first variable).
The conversion specifier should match the data type of the value.
Data Type | Conversion Specifier |
---|---|
int | %d |
double | %lf |
char | %c |
Behavior
printf
The printf
function sends everything in the format string to be displayed on the screen. For example, running a program with the line
printf("Hello World!");
would result in
being displayed to the screen.
Notice how the very next thing (the terminal prompt) is displayed right next to our output! If we want it to display on the next line, we need to add some "blank space". The Enter key would do that if we were typing in a text document, but for formatted IO we need a special symbol, called the endline or newline character: \n
.
Adding that to the end of our format string changes the behavior of the program!
printf("Hello World!\n");
produces
.
The endline character is one of many "escaped" characters that can be used in formatted IO: https://en.wikipedia.org/wiki/Escape_sequences_in_C#Escape_sequences.
To display values, especially those stored in variables, a placeholder called a conversion specifier must be included in the format string and a corresponding variable (or value or expression) must be listed after the format string. In the printf
function, the value is "read" and replaces the corresponding conversion specifier. For example:
int age = 44;
printf("Your age is %d.\n", age);
would displaywhen the code is run.
Notice that the value stored in the
age
variable is read and replaces the %d
conversion specifier when the code is run. If you want to display more values, you must add more conversion specifiers and values:int age = 44;
printf("Your age is %d. You will be %d in 10 years\n", age, age + 10);
would displaywhen the code is run. The first value
age
replaces the first conversion specifier and the second value age + 10
replaces the second conversion specifier.
scanf
The scanf
function also uses conversion specifiers in a format string, but NOTHING IN THE FORMAT STRING IS EVER DISPLAYED!. The conversion specifiers in the format string just prepare the program to receive information from the keyboard. That information is stored in variables listed after the format string. For example,
int age;
scanf("%d", &age);
would store whatever number the user types on the keyboard in the age variable.
A whole program should help see the difference!
If we want to tell the user what to do, that text should be in a preceding printf
statement (line 9). The scanf
statement on line 10 will actually store the value in the age
variable, after which point we can use that variable in the rest of our code (line 12).
When we run our program, the first printf
statement displays the prompt then the program stops
until the user enters a number followed by the Enter key (NOTE: for this class, we will always assume the user will enter a valid value).