The scanf function is used to read data from standard input and store the data into corresponding variables according to the specified format string. Specific steps include: specifying a format string, defining the format and variable type for reading data. Provide a variable pointer and pass the variable address to store the read data. Call the scanf function, passing the format string and variable pointer as arguments.
Usage of scanf function in C language
The scanf function is used in C language to read from standard input The function to get data, its syntax is as follows:
<code class="cpp">int scanf(const char *format, ...);</code>
Usage
scanf function reads data from standard input according to the specified format stringformat
And store it in the corresponding variable, and return the number of successfully read variables.
Steps
format
The string specifies the read data Format and variable types. For example: "%d"
means reading an integer, "%f"
means reading a floating point number. format
string must be passed by a pointer to the scanf
function. For example: &num
represents the address of variable num
. scanf
function and pass the format
string and variable pointer as parameters. For example: scanf("%d", &num)
. Example
Read an integer and a floating point number from the standard input. The code is as follows:
<code class="cpp">int num; float fval; scanf("%d %f", &num, &fval);</code>
Notes
scanf
The function skips leading spaces and tabs. scanf
The function does not verify that the user input is of the correct type, thus potentially causing undefined behavior. The scanf
function cannot read space characters. If you need to read spaces, you can use the getchar
or gets
function. The above is the detailed content of How to use scanf in c language. For more information, please follow other related articles on the PHP Chinese website!