C language is a very important programming language. Many beginners often use the input function scanf() in the process of learning and using C language. Through the scanf() function, we can obtain input data from the user and then store it in the specified variable. This article will introduce the input format examples and analysis of scanf(), and provide you with specific code examples.
In C language, the format control string of the scanf() function is used to specify the format of the input data. Below we will introduce different types of input data as examples.
Suppose we need to get an integer from the user and store it in the variable num. You can use the following format to control the string:
scanf("%d", &num);
In this example, %d indicates that an integer value is to be read, and &num indicates that the read integer is to be stored in the variable num.
If we need to get a floating point number and store it in the variable num, we can use the following format to control the string:
scanf("%f", &num);
In this example, %f indicates that a floating point value is to be read.
When you need to get a string, you can use the following format to control the string:
scanf("%s", str);
%s here means What is to be read is a string, and str means that the read string is to be stored in the character array str.
When you need to obtain multiple data from the user, you can use multiple format control strings to specify different types of data. For example, if we need to get an integer and a floating point number and store them into variables num1 and num2, we can use the following format to control the string:
scanf("%d%f", &num1, &num2);
In this example, %d%f means Read an integer and a floating point number respectively and store them into variables num1 and num2.
It should be noted that when using the scanf() function, if the input data does not match the data type specified by the format control string, an error will occur or unexpected results will occur. Therefore, when writing a program, we must ensure that the input data type is consistent with the specified format.
The following is a complete sample program that demonstrates how to use the scanf() function to obtain user input:
#includeint main() { int num; float fnum; char str[100]; printf("请输入一个整数:"); scanf("%d", &num); printf("请输入一个浮点数:"); scanf("%f", &fnum); printf("请输入一个字符串:"); scanf("%s", str); printf("您输入的整数是:%d ", num); printf("您输入的浮点数是:%f ", fnum); printf("您输入的字符串是:%s ", str); return 0; }
Through the above sample program, we can enter an integer at runtime , a floating point number and a string, and output them to the screen in sequence.
To summarize, the scanf() function is a very important C language input function. By specifying the format control string, we can obtain different types of input data from the user and store it in the specified variable. middle. When using the scanf() function, you need to ensure that the input data type is consistent with the specified format to avoid errors or unexpected results. I hope the analysis in this article can help everyone better understand and use the scanf() function.
The above is the detailed content of Example and analysis: input format of scanf function in C language. For more information, please follow other related articles on the PHP Chinese website!