Skip to content
Justice Tech School

Reading Input with scanf

scanf reads formatted input. Pass the address with &. Here sscanf reads from a string so the example is repeatable.

Example
#include <stdio.h>

int main(void) {
  int n; char name[20];
  sscanf("42 Ada", "%d %19s", &n, name);
  printf("%d %s\n", n, name);
  return 0;
}

Result

42 Ada

Exercise

Address of n.

scanf("%d", n);

Quiz Test

Why does scanf need &?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.