Home > Backend Development > C++ > body text

In-depth discussion of the usage and parameters of the C language scanf function

王林
Release: 2024-02-19 22:00:09
Original
812 people have browsed it

In-depth discussion of the usage and parameters of the C language scanf function

In-depth analysis of the scanf function and its parameters in C language

In C language, the scanf function is a function used to input data, which allows users to start from standard The input device (usually a keyboard) reads the data and stores it into a specified variable. This article will provide an in-depth analysis of the scanf function and its parameters, and help readers understand and master its usage through specific code examples.

  1. The basic usage of the scanf function

The basic usage of the scanf function is as follows:

int scanf(const char *format, ...);
Copy after login

The scanf function accepts a formatted string as a parameter. The character The string specifies the format of the input data, and then reads the data from the standard input device and stores it in the corresponding variable according to the format. The return value of the scanf function is the number of variables that successfully read data. For example, if the data of 3 variables is successfully read, the return value is 3.

The following is a simple sample code that demonstrates how to use the scanf function to read two integers and sum them:

#include <stdio.h>

int main() {
    int num1, num2, sum;
    printf("请输入两个整数:");
    scanf("%d%d", &num1, &num2);
    sum = num1 + num2;
    printf("两个整数的和为:%d
", sum);
    return 0;
}
Copy after login

In the above code, the format string of the scanf function "% d%d" specifies the input format of two integers. Obtain the addresses of num1 and num2 through the & operator and pass them to the scanf function as parameters, thereby saving the input integers to these two variables.

It should be noted that the format control characters (such as %d, %f, %c, etc.) in the format string of the scanf function must match the variable type to be read, otherwise the data will be read. Get the error.

  1. Detailed explanation of scanf function parameters

The scanf function can have multiple parameters, and each parameter corresponds to a variable to be read. The following are several commonly used parameter usages:

  • %d: Read an integer and store it in an int type variable.
  • %f: Read a floating point number and store it in a float type variable.
  • %c: Read a character and store it in a char type variable.
  • %s: Read a string (excluding whitespace characters) and store it into a character array.

The following is some sample code that demonstrates the specific usage of scanf function parameters:

#include <stdio.h>

int main() {
    int num;
    float realNum;
    char ch;
    char str[20];
    printf("请输入一个整数和一个浮点数:");
    scanf("%d%f", &num, &realNum);
    printf("整数:%d,浮点数:%f
", num, realNum);

    printf("请输入一个字符:");
    scanf(" %c", &ch);
    printf("字符:%c
", ch);

    printf("请输入一个字符串(不包含空白字符):");
    scanf("%s", str);
    printf("字符串:%s
", str);
    
    return 0;
}
Copy after login

In the first sample code, %d and # are used ##%fTwo format control characters read an integer and a floating point number respectively. It should be noted that there is no space separation between these two format control characters. This is because the scanf function automatically terminates input when encountering a newline character or whitespace character, so multiple variables can be read continuously.

In the second sample code, since the character array (string) in C language cannot be directly input and output, you need to use the

%c format control character to read a single character. It should be noted that a space is added before %c to prevent the newline character left by the previous scanf function from being read.

The last sample code demonstrates how to read a string and store it into a character array. Since the character array itself is a pointer, there is no need to use the

& operator in the scanf function. It should be noted that when the scanf function uses the %s format control character to read a string, it will stop reading when it encounters whitespace characters (such as spaces, tabs, newlines), so it can only read strings that do not contain whitespace characters. String.

The above is the detailed content of In-depth discussion of the usage and parameters of the C language scanf function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template