codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C user input

C user input

Read with scanf and fgets.

scanf("%d", &n) reads a number (note the &). Use fgets to read a whole line safely.

Example code

#include <stdio.h>
int main() {
  int n;
  char name[30];
  printf("Number: ");
  scanf("%d", &n);
  getchar();
  printf("Name: ");
  fgets(name, 30, stdin);
  printf("%d %s", n, name);
  return 0;
}

Run this code in the online editor

← C stringsC memory address →