scanf()
function is used to read input from the console or standard input of the application in C and C++ programming language. scanf() function can read different data types and assign the data into different variable types. The input data can be read in different formats by using format specifiers. The standard input can be also called as stdin in C and C++ terminology. The scanf() function is provided by the stdio.h
library or header.
scanf()
函数用于以C和C ++编程语言从控制台读取输入或应用程序的标准输入。 scanf()函数可以读取不同的数据类型,并将数据分配为不同的变量类型。 通过使用格式说明符,可以以不同的格式读取输入数据。 在C和C ++术语中,标准输入也可以称为stdin。 scanf()函数由stdio.h
库或标头提供。
scanf()函数语法 (scanf() Function Syntax)
As a function scanf() accepts different parameters. It has the following syntax and parameter meaning. We can also read multiple parameters with a single scanf() function. For single-parameter, only VARIABLE1
will be specified.
函数scanf()接受不同的参数。 它具有以下语法和参数含义。 我们还可以使用单个scanf()函数读取多个参数。 对于单参数,将仅指定VARIABLE1
。
int scanf(const char *FORMAT, VARIABLE1, VAARIABLE2, ...)
- `int` is the return data type which is an integer after the reading input data. This will provide the status of the reading operation. int是返回数据类型,它是读取输入数据之后的整数。 这将提供读取操作的状态。
- `scanf` is the function name. “ scanf”是函数名称。
- `const char *FORMAT` is the format specifier of the input. const char * FORMAT是输入的格式说明符。
- `VARIABLE1`, `VARIABLE2`, … are the variable name in which value will be set from the user input. “ VARIABLE1”,“ VARIABLE2”,…是变量名,将从用户输入中设置值。
scanf()返回值 (scanf() Return Value)
The scanf() function will return an integer value which is not related to the input value. If the scanf() function is completed successfully the return value will be the input parameter count. If there is an error like matching failure, reading error or reach the end of file the return value will be less than zero which value is related to the error type.
scanf()函数将返回与输入值无关的整数值。 如果scanf()函数成功完成,则返回值将是输入参数计数。 如果出现诸如匹配失败,读取错误或到达文件末尾之类的错误,则返回值将小于零,该值与错误类型有关。
scanf()函数格式说明符 (scanf() Function Format Specifiers)
Before starting the scanf() example we will provide the format specifiers. Format specifiers are used to specifying the input data format which will be set for the given variable. The format should be the same as the given variable. There are different format specifiers for different variable types like below.
在开始scanf()示例之前,我们将提供格式说明符。 格式说明符用于指定将为给定变量设置的输入数据格式。 格式应与给定变量相同。 对于不同的变量类型,有不同的格式说明符,如下所示。
- `%c` is used to input character input for `char` variable type %c用于为char类型的变量输入字符
- `%d` is used to input integer input for `int` variable type%d用于为int变量类型输入整数输入
- `%e`,`%E`,`%f`,`%g`,`%G` are used to input floating number for `float` variable type%e,%E,%f,%g,%G用于输入float类型的浮点数
- `%o&