The scanf()
function in C++ is used to read the data from the standard input (stdin
). The read data is stored in the respective variables.
It is defined in the cstdio header file.
Example
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
// get age from user
scanf("%d", &age);
// print age
cout << "Age = " << age;
return 0;
}
Output
Enter your age: 39 Age = 39
scanf() Syntax
The syntax of scanf()
is:
scanf(const char* format, ...);
scanf() Parameters
The scanf()
function takes the following parameters:
- format - pointer to a C-string that specifies how to read the input. It consists of format specifiers starting with
%
.
- ... - other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.
scanf() Return Value
- If successful, it returns the number of receiving arguments successfully assigned.
- If a matching failure occurs before the first receiving argument was assigned, returns
0
. - If input failure occurs before the first receiving argument was assigned,
EOF
is returned.
scanf() Prototype
The prototype of the scanf()
function as defined in the cstdio header file is:
int scanf(const char* format, ...);
Format String
The format parameter of scanf()
can contain format specifiers that begin with %
. The format string has the following parts:
- Non whitespace characters except
%
each of which consumes one identical character from the input stream. It can cause the function to fail if the next character on the stream does not compare equal. - Whitespace character - All the consecutive whitespace characters are treated as a single whitespace character. Further,
'\n'
,'\t'
and' '
are considered the same. - Conversion specification - It follows the following format:
- Initial
%
character that specifies the beginning. - An optional
*
called assignment-suppression character. If this character is present,scanf()
does not assign the result to any receiving argument. - width - an optional positive integer number that specifies maximum field width. It specifies the maximum number of characters that
scanf()
is allowed to consume when doing the conversion specified by the current conversion specification. - length - an optional length modifier specifying the size of the receiving argument.
- specifier - a conversion format specifier.
- Initial
...
- other additional arguments for receiving data. They occur in a sequence according to the format specifier.
scanf() Format Specifier Prototype
The general format of format specifier for scanf()
is:
%[*][width][length]specifier
Common Format Specifiers
Format Specifier | Description |
---|---|
% |
matches the literal % |
c |
- matches a single character or multiple characters - if width is defined, it matches exactly width characters |
s |
- matches consecutive non-whitespace characters - if width is defined, matches exactly width characters or until the first whitespace is found |
[set] |
- matches a non-empty sequence of characters from the given set of characters - if ^ is present at the beginning of set , then all the characters not in set are matched |
d |
matches a decimal integer |
i |
matches an integer |
o |
matches an unsigned octal integer |
X or x |
matches an unsigned hexadecimal integer |
u |
matches an unsigned decimal integer |
A or a |
matches a floating-point number |
E or e |
matches a floating-point number |
F or f |
matches a floating-point number |
G or g |
matches a floating-point number |
n |
returns the number of characters read so far |
p |
matches an implementation defined character sequence defining a pointer |
Example: C++ scanf()
#include <cstdio>
int main () {
char name[50];
int age;
printf("Enter your name: ");
// get user input for name
scanf("%s", name);
printf("Enter your age: ");
// get user input for age
scanf("%d", &age);
// print name and age
printf("Hello %s, you are %d years old.", name, age);
return 0;
}
Output
Enter your name: Tim Enter your age: 31 Hello Tim, you are 31 years old.