Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

C User Input

Programs become interactive when they read what the user types. In the online editor, run the program, then type your answer in the terminal and press Enter.

scanf

scanf reads values in the same formats printf prints them. The variable must be given with an ampersand & in front: scanf needs the variable's address to store the value there.

#include <stdio.h>

int main(void) {
    int age;
    printf("How old are you? ");
    scanf("%d", &age);
    printf("Next year you will be %d.\n", age + 1);
    return 0;
}
You type
12
Output
How old are you? Next year you will be 13.

Forgetting the & is the most common scanf mistake: the program may crash. The compiler warns, and the editor's hint points it out.

Typescanf format
int%d
float%f
double%lf (note the l)
char%c (use " %c" with a space to skip the Enter)
word (string)%s, without &

Several values

#include <stdio.h>

int main(void) {
    int a, b;
    printf("Two numbers: ");
    scanf("%d %d", &a, &b);
    printf("%d + %d = %d\n", a, b, a + b);
    return 0;
}
You type
4 9
Output
Two numbers: 4 + 9 = 13

Checking the input

scanf returns how many values it read. If the user types letters where a number is expected, it returns fewer, and the variable keeps its old (garbage) value. Always check:

#include <stdio.h>

int main(void) {
    int n;
    printf("A whole number: ");
    if (scanf("%d", &n) != 1) {
        printf("That is not a whole number.\n");
        return 1;
    }
    printf("You typed %d.\n", n);
    return 0;
}
You type
abc
Output
A whole number: That is not a whole number.

(In our editor the terminal also prints an input error note when scanf reads fewer values than its format asks for.)

Words and whole lines

%s reads one word: it stops at the first space. Strings are already addresses, so there is no &. Give a maximum width (%19s for an array of 20) so a long word cannot overflow the array.

To read a whole line with spaces, use fgets(buffer, size, stdin). It keeps the Enter ('\n') at the end, which strcspn removes neatly. Never use gets: it cannot know the buffer size and was removed from C.

#include <stdio.h>
#include <string.h>

int main(void) {
    char name[40];
    printf("Your full name: ");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';   // remove the Enter
    printf("Hello, %s!\n", name);
    return 0;
}
You type
Asha Kumar
Output
Your full name: Hello, Asha Kumar!

Mixing scanf and fgets has a trap: after scanf("%d", ...) the Enter is still waiting, and the next fgets reads that empty line. Read a line with fgets and convert it with sscanf, or skip the rest of the line first.

Practice exercises

  1. Read the length and width of a rectangle and print its area and perimeter.
  2. Read a temperature in Celsius as a double (%lf) and print it in Fahrenheit.
  3. Read two numbers and print a clear message when the user does not type two numbers.
  4. Read a full sentence with fgets and print how many characters it has.