C scanf Function
C scanf Function
The C scanf function is a standard library function. The Standard Input/Output Library contains functions for formatted data transfer in and out of the C program. The two such functions are as follows:
- printf (print formatted) function
- scanf (scan formatted) function
The data printing and reading functions are defined as standard functions which are part of
the language specification but are not part of the C Language itself. The functions are defined in the system header file stdio.h
scanf Function
The C scanf function is a standard library function to read data from the standard input. This function is the counterpart of the printf function.
The scanf() function reads input from stdin, according to the given format, and stores the data in the other arguments.
Syntax
The general syntax of the scanf function is:
scanf(format, arg1,arg2,…);
The format string contains the conversion specifications. The arguments should be pointers to indicate where the input should be stored.
Examples
Read an Integer
To read an integer and store it into an integer variable called a number.
scanf(“%d”,&number);
The & is the ‘address of‘ operator in the C language. The &number returns the address of the variable number.
Read float Number
In this example, we will use the scanf function to read data into float variable length.
scanf(“%f”,&length);