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

Syntax Edit

int scanf( 
  const char *format [,

Parameters Edit

Format - Format of the incoming data

Argument - Optional variable outputs

Usage Edit

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);


Note Edit

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

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.