Scanning numbers

To read input, there is scanf, analogous to printf. Like printf, a call to scanf starts with a format string. For each conversion specification, there is a corresponding argument and the input from the user will be converted appropriately and saved in that destination.

Rather than computing \(2+2\), for example, we could read in an arbitrary number \(n\), and compute \(n+n\). The conversion specifier for reading a number in decimal is d, and the corresponding destination must be an int variable. (In order for scanf to change the variable to hold the input value, it has to be passed as a pointer; we will discuss pointers in more detail, but for now, think of &n as telling scanf where to find the variable n in memory, as opposed to just its value.)

#include <stdio.h>

int main()
{
    int n;
    int x;

    scanf("%d", &n);

    x = n + n;
    printf("%d + %d = %d\n", n, n, x);
}

If you want to prompt the user rather than just waiting for them to guess what to type, you can use printf and end the message with a space rather than a newline, so the user can type on the same line.

#include <stdio.h>

int main()
{
    int n;
    int x;

    printf("Enter a number: ");
    scanf("%d", &n);

    x = n + n;
    printf("%d + %d = %d\n", n, n, x);
}

As with printf, most types have a corresponding conversion specifier in a scanf format string, and they are similar to printf’s language but not identical. For example, although in printf the f specifier prints a double, in scanf the f specifier scans a float. To scan a double, you have to tell scanf to use the longer type with lf. (There is a good reason for this inconsistency, and it’s a very CS205 reason, but for now I’m just trying to give you the tools to read and write numbers, so we’ll put off the deeper explanation.)

#include <stdio.h>

int main()
{
    double celsius;
    double fahrenheit;

    scanf("%lf", &celsius);
    fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    printf("%f Celsius is %f Fahrenheit\n", celsius, fahrenheit);
}

You might have noticed that these examples have split up declaring variables from assigning them their initial values. This is often the result of needing to declare variables before they are used, even to fill them in with values from input. In C, variables are typically declared in one paragraph at the top of a scope. If you are used to C++, you might expect to be able to declare variables on demand throughout a scope, something more like the following.

#include <stdio.h>

int main()
{
    double celsius;
    scanf("%lf", &celsius);

    double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    printf("%f Celsius is %f Fahrenheit\n", celsius, fahrenheit);
}

Modern C does allow this style, but it is a back-formation from C++, and I consider it more C-ish to keep to the old style with declarations at the top. Arguably, C++ had a good idea there, though, and it is part of the C language standard now, so go with the style you prefer.

You have attempted of activities on this page