Explanation of C language input and output functions

C language input and output

The input and output in the C language can use the standard library functions scanf and printf, which are used to read user input and output results to the console respectively.

input example

Below is an example of reading user input from the console. The program will ask the user to enter an integer and then print it to the console.

#include <stdio.h>

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

output example

Below is an example that prints strings and variables to the console. The program will print the string "Hello World!" and an integer to the console.

#include <stdio.h>

int main() {
    int num = 42;
    printf("Hello World!\\n");
    printf("我的年龄是:%d岁\\n", num);
    return 0;
}

Below is an example of outputting strings and variables to the console. The program outputs "Hello World!" and an integer to the console.

#include <stdio.h>

int main() {
    int num = 42;
    printf("Hello World!\\\\n");
    printf("我的年龄是:%d岁\\\\n", num);
    return 0;
}

Note that escape characters are used in strings to represent special characters. For example, \\\\nto represent a newline character.

The C language standard library function scanf is used to read user input from the console. Below is an example that reads two integers from the console.

The C language standard library functions getchar() and putchar() are used to read a single character from the console and output a single character to the console, respectively. Below is an example that reads characters from the console and outputs them to the console.

#include <stdio.h>

int main() {
    char c;
    printf("请输入一个字符:");
    c = getchar();
    printf("您输入的字符是:");
    putchar(c);
    return 0;
}

The program prompts the user for a character and then prints that character to the console.
Note that when using the getchar() function, a character variable needs to be declared to store the read character.

In the C language standard library, there are two commonly used input and output functions: gets() and puts(). The gets() function is used to read a line of string from the console, and the puts() function is used to output the string to the console. Below is a sample program using the gets() and puts() functions.

#include <stdio.h>

int main() {
    char str[50];
    printf("请输入一行字符串:");
    gets(str);
    printf("您输入的字符串是:");
    puts(str);
    return 0;
}

The program prompts the user for a line of string and then prints that string to the console.

Note that when using the gets() function, you need to declare a character array to store the read string, and you need to ensure that the length of the input string does not exceed the length of the array, otherwise it may cause buffer overflow. In order to avoid this situation, you can use the fgets() function instead of the gets() function, which can specify the maximum number of characters to read, thereby avoiding the problem of buffer overflow.

In addition, the puts() function automatically adds a newline character at the end of the string, so there is no need to use escape characters in the string to represent newline characters.

In C language, the standard library functions scanf and printf can be used to read user input and output to the console.

scanf is used to read user input from the console. Below is an example that reads two integers from the console.

#include <stdio.h>

int main() {
    int num1, num2;
    printf("请输入两个整数,以空格分隔:");
    scanf("%d %d", &num1, &num2);
    printf("您输入的两个整数是:%d 和 %d", num1, num2);
    return 0;
}

printf is used to output the result to the console. Below is an example that prints strings and variables to the console. The program will print the string "Hello World!" and an integer to the console.

#include <stdio.h>

int main() {
    int num = 42;
    printf("Hello World!\\n");
    printf("我的年龄是:%d岁\\n", num);
    return 0;
}

Note that escape characters are used in strings to represent special characters. For example, \\nto represent a newline character.

In addition, the C language standard library functions getchar() and putchar() are used to read a single character from the console and output a single character to the console, respectively. Below is an example that reads characters from the console and outputs them to the console.

#include <stdio.h>

int main() {
    char c;
    printf("请输入一个字符:");
    c = getchar();
    printf("您输入的字符是:%c", c);
    return 0;
}

The program prompts the user for a character and then prints that character to the console.

In the C language standard library, there are two commonly used input and output functions: gets() and puts(). The gets() function is used to read a line of string from the console, and the puts() function is used to output the string to the console. Below is a sample program using the gets() and puts() functions.

#include <stdio.h>

int main() {
    char str[50];
    printf("请输入一行字符串:");
    gets(str);
    printf("您输入的字符串是:");
    puts(str);
    return 0;
}

The program prompts the user for a line of string and then prints that string to the console.

Note that when using the gets() function, you need to declare a character array to store the read string, and you need to ensure that the length of the input string does not exceed the length of the array, otherwise it may cause buffer overflow. In order to avoid this situation, you can use the fgets() function instead of the gets() function, which can specify the maximum number of characters to read, thereby avoiding the problem of buffer overflow.

In addition, the puts() function automatically adds a newline character at the end of the string, so there is no need to use escape characters in the string to represent newline characters.

Guess you like

Origin blog.csdn.net/weixin_51624736/article/details/129500503