Input

I/O Input and Output #

When we are working with our programs, one of the most important aspects of them is to interact with the user in some way. At the start of this module we are dealing with text based user interfaces, also called terminal interfaces, or command line interfaces. These types of interface mainly consist of printing text, and reading in text from the keyboard.

The stdio.h file that we include at the top of our programs contains two functions, printf and scanf that we will use for printing to the screen, and scanning from the keyboard. You will have noticed the print/scan terminology is used in a similar manner when talking about document printers and scanners.

puts #

I’m going to briefly mention the puts function, which prints a string of text to the console. The puts function can only print out text exactly as we give it, and it will also add a newline at the end of it, so that subsequent printing happens on the following line. The puts function is not as versatile as printf and mixing the two up can cause confusion so we will only use printf. This section is included so that you are aware that puts exists and that you will understand it when you see it used by others.

puts("Hello, world!");

The above code will simply print out Hello, World! (without the quotes) to the screen, followed by a newline.

printf #

The printf function is what we will use to print out information to the screen. printf allows us to print formatted text, and it allows us to not only print text, but to also print out the value of variables. The formatting is very limited on the console, and we can really only format variables for display, add tabs, and newlines. The printf function also returns an integer, the number of characters that it printed to the screen. This can be useful for when we want to add some spacing around our output, but we won’t be dealing with that just yet.

There are a number of different ways that printf can be called. All function calls, not just printf, take the same format: they begin with the name of the function, then parenthesis (), and inside the parenthesis a list of values that the function needs to perform its task. At its most basic, printf needs a piece of text to print out:

printf("Hello, World!");

The above code will print out Hello, World! (without the quotes), and the next call to printf will continue printing at the location that the previous call finished. The printf function doesn’t print a newline unless we explicitly tell it to by putting \n in our string:

printf("Hello\nWorld!");

The above code will output the following:

Hello
World!

You can see that the first line outputs Hello, then it encounters a \n, the newline character, which means move down to the next line, and then it prints the rest of the string World!.

Printing the value of variables #

One of the most useful features of print, is that it allows us to easily print out the value of variables to the screen. Since we have only worked with integers (int) so far, we will be using the %d format specifier to print out the value of integer variables. When we introduce new types of variables, there will be a section on using printf and scanf with them.

We can print out as many variable values as we want using printf, and each one of them is matched to a format specifier.

int a = 5;
printf("The value of a is: %d", a);

This matches the first format specifier it finds, %d with the first variable that it finds, a, and it then replaces the format specifier with a text representation of the value of a. This will print out the following to the screen:

The value of a is: 5

If we have multiple variables we can print them out by adding more format specifiers and more variables. Take note of the commas and where the variables are added. Format specifiers are added in the string of text, and variables are added after that, separated by commas. These are positional, so the first format specifier will match the first variable.

int a=5, b=30;
printf("The time is %d:%d", a, b);

This will match the first %d to a and the second %d to b and so on. scanf works on a similar basis.

scanf #

One of the ways of reading input from the keyboard is by using the scanf function. The scanf function is one of the most beginner friendly ways of reading input from the keyboard but it is not the most secure. As we progress over the next few months and years we will move on to other more secure functions and explore how the scanf function can be exploited by hackers for nefarious purposes.

With that said, scanf is one of the easiest ways to read input from the keyboard, you call the function, and give it a string complete with format specifiers, and the variables in which you want to store the inputted values.

When using scanf instead of simply giving the variables name to the function, we need to give it the variables memory address so that it can put the value into the variable as opposed to give it the value of the variable, which is often uninitialized. This is a more advanced topic in C, and we won’t be diving into it now, so all you need to know is that when using scanf we need to put an & in front of the variable name in order to give its memory address, instead of its value.

Don’t confuse printf and scanf!
One of the main problems that new programmers encounter is accidentally putting an & in front of their variables in printf function calls, or forgetting to put it in their scanf function calls! Make sure that you are aware of the differences.

To read in an integer, we simply give it the format specifier for an integer, and then the address of the variable into which we want to store the inputted value.

int num;
printf("Please enter a number: ");
scanf("%d",&num);

When using scanf it is always a good idea to use a printf first to tell the user what you are expecting them to do. You don’t technically need to do this, there is no interdependence on printf and scanf.

We can also use scanf to read in multiple values, like we did with printf:

int day, month, year;
printf("Please enter your date of birth: ");
scanf("%d/%d/%d",&day, &month, &year);

The above code will require the user to type in an input matching the string in quotes, so even though a user will have to enter the forward slashes between the dates, they are not stored and only the %d part of them is read in.

scanf also returns a value when its execution has completed, the number of items that it read in successfully. This is important for the correctness of our program as we must verify that the items were read in correctly. In the date example above we need to be sure that all of the 3 integers were entered correctly before we proceed.