0Pricing
C Academy · Lesson

scanf and Input

Read user input.

What scanf Does

scanf reads formatted input typed by the user. It uses the same specifiers as printf.

#include <stdio.h>
int main(void) {
    int x = 0;
    /* scanf("%d", &x); */
    x = 5;
    printf("x = %d\n", x);
    return 0;
}

The Address-Of Operator

scanf needs the address of a variable so it can store the value there. That is why you write &x.

#include <stdio.h>
int main(void) {
    int x = 42;
    int *p = &x;
    printf("value via address = %d\n", *p);
    return 0;
}

All lessons in this course

  1. printf Format Specifiers
  2. scanf and Input
  3. Width, Precision, Flags
  4. Common I/O Pitfalls
← Back to C Academy