Home  >  Article  >  Backend Development  >  C language input format using scanf function

C language input format using scanf function

WBOY
WBOYOriginal
2024-02-25 18:00:11762browse

C language input format using scanf function

C language is a language widely used in the field of programming. It is simple, flexible and efficient. In C language, we often use the scanf function for input operations, which allows users to input data and assign values ​​to variables. This article will introduce how to use the scanf function and provide specific code examples.

The formatted input of the scanf function is completed according to the conversion specifier. When using the scanf function, the conversion specifier is used to specify the type of data to be input. The following are some commonly used conversion specifiers and their corresponding data types:

  • %d: used to input integer data.
  • %f: used to input floating point data.
  • %c: used to input character data.
  • %s: used to input string data.

When using the scanf function for input operations, we need to specify the input data type according to the predefined format. Below are some examples of common input operations.

Example 1: Input integers

#include <stdio.h>

int main() {
    int num;
    printf("请输入整数:");
    scanf("%d", &num);
    printf("您输入的整数是:%d
", num);
    return 0;
}

In the above example, we used %d to indicate that the input is integer data. Through the scanf function, we can store the integer entered by the user into the variable num, and then output the value of the variable.

Example 2: Input floating point numbers

#include <stdio.h>

int main() {
    float num;
    printf("请输入浮点数:");
    scanf("%f", &num);
    printf("您输入的浮点数是:%f
", num);
    return 0;
}

In the above example, we used %f to indicate that the floating point data is to be input. Through the scanf function, we can store the floating point number entered by the user into the variable num, and then output the value of the variable.

Example 3: Enter characters

#include <stdio.h>

int main() {
    char ch;
    printf("请输入字符:");
    scanf(" %c", &ch);
    printf("您输入的字符是:%c
", ch);
    return 0;
}

In the above example, we used %c to indicate that the character data to be input is character type. It should be noted that adding a space before %c is to filter out previous line breaks or spaces.

Example 4: Input string

#include <stdio.h>

int main() {
    char str[50];
    printf("请输入字符串:");
    scanf("%s", str);
    printf("您输入的字符串是:%s
", str);
    return 0;
}

In the above example, we used %s to indicate that the string data is to be input. Through the scanf function, we can store the string entered by the user into the character array str, and then output the value of the array.

Summary: This article introduces the method of using the scanf function for input operations in C language, and provides specific code examples. By mastering this knowledge, we can better utilize the scanf function for input operations and improve programming efficiency. Hope this article is helpful to everyone.

The above is the detailed content of C language input format using scanf function. 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