Home > Article > Backend Development > Detailed explanation of how to use scanf function in C language
Detailed explanation and code examples of the use of scanf function in C language
C language is a programming language widely used in various software development, in which input and output Functions play a very important role in writing programs. Among them, the scanf function is one of the functions in C language used to read standard input. It can read data from the keyboard according to a specific format and store it in a specified variable. This article will introduce the use of the scanf function in detail and provide some example codes for examples.
First, let’s take a look at the basic syntax of the scanf function:
int scanf(const char* format, ...);
Among them, format is a string used to specify the format of the input data. For different types of data, corresponding placeholders need to be used in the format string to identify them, such as: %d represents integer type, %f represents floating point type, %c represents character type, %s represents string, etc. And... means that any number of parameters can be accepted. These parameters are variables used to receive input. Note that the order of the variables must correspond to the placeholders in the format string.
The following are some common examples to illustrate the use of the scanf function:
Read integers
#include <stdio.h> int main() { int a; printf("请输入一个整数:"); scanf("%d", &a); printf("你输入的整数是:%d ", a); return 0; }
The above code first outputs the prompt information, Ask the user to enter an integer, then use the scanf function to read the entered integer and store it into variable a. Finally, output the value of a again. It should be noted that in the scanf function, the address character & must be added in front of the variable a to indicate that the input value is stored in the memory address corresponding to a.
Read floating point number
#include <stdio.h> int main() { float f; printf("请输入一个浮点数:"); scanf("%f", &f); printf("你输入的浮点数是:%f ", f); return 0; }
The above code is similar to the first example, except that the type of variable a is changed to float, and the format string is also changed accordingly. Adjustment.
Read characters
#include <stdio.h> int main() { char c; printf("请输入一个字符:"); scanf(" %c", &c); printf("你输入的字符是:%c ", c); return 0; }
It should be noted that there is a space before %c in the format string, which is to ignore the previous newline character. When using %c to read characters, if the characters entered by the user include a newline character, they will be regarded as input characters.
Read string
#include <stdio.h> int main() { char str[100]; printf("请输入一个字符串:"); scanf("%s", str); printf("你输入的字符串是:%s ", str); return 0; }
When reading a string, you can use the %s placeholder, which will save the string entered by the user in the specified in character array. Here we define a character array str with a length of 100 to store the input string.
The above examples are just some basic usages of the scanf function. In fact, the scanf function can also support more complex format strings and more data types. In practical applications, we can also achieve more powerful functions by combining different format strings and control characters, such as supporting input of multiple values, skipping certain characters, etc.
In short, the scanf function is one of the very important and commonly used functions in the C language. It can help us read data in a specific format from the standard input stream and save it to a specified variable. Through the introduction of this article, I hope readers can better understand and master the use of the scanf function, and be able to use it flexibly in actual programming.
The above is the detailed content of Detailed explanation of how to use scanf function in C language. For more information, please follow other related articles on the PHP Chinese website!