Home > Common Problem > What are the input functions in C language?

What are the input functions in C language?

小老鼠
Release: 2023-10-11 11:41:11
Original
2828 people have browsed it

C language input functions include scanf function, getchar function, gets function, fgets function, scanf_s function, etc. Detailed introduction: 1. The scanf function is used to read data from the standard input stream. It can convert the input data into the corresponding type according to the specified format string and store it in the specified variable; 2. getchar function , used to read a character from the standard input stream, it will wait for the user to input a character, and return the ASCII code value of the character; 3. gets function, etc.

What are the input functions in C language?

#C language is a widely used programming language whose input functions are used to obtain data from the user or external devices. C language provides a variety of input functions, commonly used ones include:

1. scanf function: The scanf function is used to read data from the standard input stream (usually the keyboard). It can convert the input data into the corresponding type according to the specified format string and store it in the specified variable. For example, scanf("%d", &num) can read an integer and store it in the variable num.

2. getchar function: The getchar function is used to read a character from the standard input stream. It waits for the user to enter a character and returns the ASCII code value of the character. For example, char ch = getchar() reads a character and stores it into the variable ch.

3. gets function: gets function is used to read a line of string from the standard input stream. It stores the input string into the specified character array until a newline character is encountered. For example, char str[100]; gets(str) can read a line of string and store it into the str array.

4. fgets function: The fgets function is used to read a line of string from the specified input stream. It is similar to the gets function, but you can specify the maximum number of characters to read, and you can read data from input streams other than the standard input stream. For example, char str[100]; fgets(str, sizeof(str), stdin) can read a line of string from the standard input stream and store it into the str array.

5. scanf_s function: The scanf_s function is a safe version of the scanf function introduced in the C11 standard. It is similar to the scanf function, but you can specify the maximum number of characters to read to avoid buffer overflow problems. For example, scanf_s("%s", str, sizeof(str)) can read a string and store it into the str array.

In addition to the above-mentioned commonly used input functions, C language also provides other input functions, such as sscanf function (reading data from strings), fscanf function (reading data from files), etc. These functions can be selected and used according to specific needs.

To summarize, the input functions of C language include scanf, getchar, gets, fgets, scanf_s, etc. They can easily obtain data from users or external devices and process it accordingly. When using these functions, you need to pay attention to the matching of input format and data type to ensure the correctness and security of the input.

The above is the detailed content of What are the input functions in C language?. 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