Programmer's Wiki

Scanf is a function that reads input data in text based console programs.  It is declared in stdio.h.


int scanf( 
  const char *format [,


Format - Format of the incoming data

Argument - Optional variable outputs


This function will be used when input is required in console programs when using stdio.  This will receive all input on the line until the enter key is pressed.  It will then place the value into the variables you have declared.  This is an example:

#include <stdio.h>           // Standard I/O functions (printf, scanf, etc)
#include <stdlib.h>          // EXIT_SUCCESS and EXIT_FAILURE (slightly more portable than 0 and 1)
#include <ctype.h>           // isalpha() function

int main()
	char c;                         // Variable to store the user's input
	printf("Enter a letter: ");
	scanf("%c", &c);                // Here we pass a pointer to 'c' to scanf()

	if( !isalpha(c) ) {             // Test if 'c' is a letter
		fprintf(stderr, "error: '%c' is not a letter!\n", c);
		return EXIT_FAILURE;    // Quit
	} else printf("You typed the letter '%c'!\n", c);



This function is sometimes unsafe because it takes the input and places it into the variable defined.  This can cause buffer overload unless you use a width for the format, or use scanf_s