Home  >  Article  >  Backend Development  >  How to use scanf to input string in C language

How to use scanf to input string in C language

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-16 11:41:4436719browse

In C language, you can use the "scanf ("format control string", variable memory address)" statement to input a string. The first parameter of the scanf() function is a format string, which specifies the input format, parses the input corresponding position information according to the format specifier, and stores it in the position pointed to by the corresponding pointer in the variable parameter list.

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

General form of scanf function

The scanf function is a standard library function, and its function prototype is in the header file "stdio.h". Like the printf function, the C language also allows the stdio.h file to not be included before using the scanf function. The general form of the scanf function is:

scanf(“格式控制字符串”, 地址表列);

Among them, the format control string has the same function as the printf function, but it cannot display non-format strings, that is, it cannot display prompt strings. The address of each variable is given in the address table column. The address is composed of the address operator "&" followed by the variable name.

For example: &a and &b represent the addresses of variable a and variable b respectively.

This address is the address allocated by the compilation system to the a and b variables in the memory. In C language, the concept of address is used, which is different from other languages. The two different concepts of variable value and variable address should be distinguished. The address of the variable is allocated by the C compilation system, and the user does not need to care about the specific address.

The relationship between the address of the variable and the variable value

Assign a value to the variable in the assignment expression, such as:

a=567;

Then, a is the variable name, 567 is the value of the variable, &a is the address of variable a.

But on the left side of the assignment number is the variable name, and the address cannot be written. The scanf function essentially assigns a value to the variable, but it requires the address of the variable to be written, such as &a. The two are different in form. & is an address-taking operator, &a is an expression whose function is to find the address of a variable.

#include <stdio.h>
int main(void){
    int a,b,c;
    printf("input a,b,c\n");
    scanf("%d%d%d",&a,&b,&c);
    printf("a=%d,b=%d,c=%d",a,b,c);
    return 0;
}

In this example, since the scanf function itself cannot display the prompt string, the printf statement is first used to output the prompt on the screen, asking the user to enter the values ​​​​of a, b, and c. Execute the scanf statement and wait for user input. In the format string of the scanf statement, there is no non-format character between "%d%d%d" as an input interval. Therefore, more than one space or enter key must be used as an input interval between each two input numbers. interval. Such as:
7 8 9
or
7
8
9

Recommended tutorial: "C#"

The above is the detailed content of How to use scanf to input string in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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