scanf function reads data from standard input into variables in the specified format. Format specifiers specify data types, such as %d (integer), %c (character), %f (floating point), %s (string). The function returns the number of variables read, EOF indicates end of file or error.
Introduction to scanf function
The scanf function is used in C language to read from standard input (usually the keyboard) Get formatted data. It reads data from the input stream in the given format and stores it in the specified variable.
Function prototype
<code class="c">int scanf(const char *format, ...);</code>
Parameters
format
: a format string , describes the data type and format to be read. ...
: A variable number of pointers pointing to variables to store read data. Return value
The scanf function returns the number of variables successfully read. If an EOF (end of file) or error is encountered, it returns EOF.
Format specifiers
The format string consists of the following format specifiers:
Specifiers | Data type | Example |
---|---|---|
Characters | %c | |
Decimal integer | %d | |
Floating point number | %f | |
String | %s | |
Hex integer | %x |
<code class="c">int age;
char name[20];
scanf("%d %s", &age, name);</code>
and name
variables.
The format specifier must match the data type to be read.
The above is the detailed content of C language ~ what does scanf mean?. For more information, please follow other related articles on the PHP Chinese website!