How can characters be read in the C language?
In C language, you can read a string of characters using the scanf function. The scanf function requires the use of the format specifier %s to read the string, in the following format:
scanf("%s", 字符数组名);
Here is an example code demonstrating how to read a string of characters and print it out.
#include <stdio.h>
int main() {
char str[100];
printf("请输入一串字符:");
scanf("%s", str);
printf("您输入的字符是:%s\n", str);
return 0;
}
In the above code, we start by defining a character array called ‘str’ with a size of 100 to store user input characters. Then we use the scanf function to read the user input characters and save them in ‘str’. Finally, we use the printf function to print the read string.
It is important to note that the scanf function stops reading when it encounters blank spaces or newline characters, so the input cannot contain spaces. If you need to read an entire line of characters that includes spaces, you can use the fgets function.