Reading Keyboard Input in C

The usefulness of a program lies in its ability to process data. To do this, the program must be able to read input data, process it, and return results as output.

Input data can be read from a file, exchanged over a network, or entered by the user via the keyboard. In this lesson, we will focus on the latter case by using the scanf function to read input from the keyboard.

Introduction to the scanf Function

To read input from the keyboard, the C standard library provides a function that acts as the counterpart to the printf function for screen output. This function is scanf, and it allows reading data entered by the user.

Just like printf, the f in scanf stands for formatted, indicating that just as printf needs to know the type of data to print, scanf also needs to know the type of data to read. This is done through the use of so-called format specifiers.

In fact, as an argument to scanf, we must provide a format string that specifies the type of data to read and where to store it.

For now, let's limit ourselves to reading values of type int or double.

Suppose we want to read an integer from the keyboard and store it in a variable x. We can write the following code:

int x;
scanf("%d", &x);

In the code above, %d is the format specifier for an integer, exactly like in the case of printf. We are telling scanf to read an integer and store it in the variable x, which is the second argument.

What remains to be explained is the meaning of the & symbol before the variable x. At this point in the guide, it's too complex to explain and we will return to it when we study pointers. However, we can say that it is used to pass the memory address of the variable x to the scanf function. What's important to know is that whenever we want to use scanf, we must place the & symbol in front of the variables we want to read.

To read a value of type double, the code is similar:

double y;
scanf("%lf", &y);

Again, %lf is the format specifier for a double. And again, we have used the & symbol before the variable y.

Definition

Reading an Integer or Double from the Keyboard

To read an integer or a double from the keyboard, you must use the scanf function by specifying the type of data to read using a format specifier.

The general syntax is as follows:

int x;
scanf("%d", &x);
double y;
scanf("%lf", &y);

Example Program

Now that we know how to both print to the screen and read from the keyboard, we can finally write a program that doesn't just print a message, but interacts with the user.

Suppose we want to create a program that asks the user for the base and height of a triangle and calculates its area. The code is as follows:

#include <stdio.h>

int main() {
    /* Declarations */
    double base, height, area;

    /* Input */
    printf("Enter the base of the triangle: ");
    scanf("%lf", &base);

    printf("Enter the height of the triangle: ");
    scanf("%lf", &height);

    /* Area calculation */
    area = base * height / 2;

    /* Output */
    printf("The area of the triangle is: %lf\n", area);

    return 0;
}

In this program, we applied everything we've learned so far in these initial lessons on the C language:

  • we declared double type variables to store the base, height, and area of the triangle;
  • we asked the user to enter the base and height of the triangle;
  • we calculated the area of the triangle;
  • we printed the result to the screen.

Running the program would result in an output like this:

Enter the base of the triangle: 5
Enter the height of the triangle: 3
The area of the triangle is: 7.500000

In Summary

In this lesson, we've added another piece to our understanding of the fundamentals of the C language: we've learned how to read input from the keyboard using the scanf function.

Although we haven't studied all its details, we have learned how to read int and double values and store them in variables. This already allows us to create simple but useful programs that go beyond just printing messages to the screen.

In the next lesson, we will study another fundamental concept in C: defining constants.