scanf : <stdio.h> library function

The function int scanf(const char *format, ...); reads formatted data from stdin and stores them in the variables pointed by the additional arguments. Additional arguments must point to variables of the same type as specified in the format.

Format is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.

The syntax of the format specifier is as follows: [*][width][length]type

Below is the description of each component of format specifier

Component Description
* This is an optional starting asterisk indicates that the data to be read from stdin but will be ignored. It won't get stored in any memory location.
width This specifies the maximum number of characters to be read from stdin for this read operation.
length Specifies a size different from the data pointed by the corresponding additional argument.
type A character specifying the type of data to be read from stdin and in what format it is expected to be read.

scanf type specifiers

Specifier Description
d Decimal integer. Any number of decimal digits (0-9), optionally preceded by a '+' or '-' sign.
u Unsigned decimal integer.
o Octal integer. Any number of octal digits (0-7), optionally preceded by a '+' or '-' sign.
x Hexadecimal integer. Any number of hexadecimal digits (0-9, a-f, A-F), and optionally preceded by a '+' or '-' sign.
f, e or g Floating point number. A decimal number, may containing a decimal point, optionally preceded by a '+' or '-' sign and optionally followed by the e or E character and a decimal integer.
c Character Reads a character from stream. If a width greater than one is specified, the function reads width characters and stores them in the successive locations of the array passed as argument.
s Sequence of Characters It will keep on reading characters from stream until it finds a white space character(whitespace characters are considered to be blank, newline and tab). A terminating null character ('\0') is added at the end of the stored characters.
[characters] It will read only those characters specified between the brackets.
[^characters] It will read only those characters which is not specified between the brackets.

Function prototype of scanf

int scanf(const char *format, ...);
  • format : This is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.
  • additional arguments : As per the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the data read from stdin to be stored.

Return value of scanf

On success, scanf function returns the total number of objects successfully read, it may or may not be same as the expected number of items specified in format string.

C program to show the use of scanf function

The following program shows the use of scanf function to read formatted data from a stdin.

#include <stdio.h>

int main(){
    int a, b, sum;
    printf("Enter to integers to add\n");
    /* Taking input from user using scanf */
    scanf("%d %d", &a, &b);
    sum = a + b;
    
    printf("%d + %d = %d", a, b, sum);

    return 0;
}

Program Output
Enter to integers to add
3 9
3 + 9 = 12