Reading Multiple Characters in C

In the C programming language, the scanf function can be used to read multiple characters. Here is a simple example code:

#include <stdio.h>

int main() {
    char str[100]; // 定义一个足够大的字符数组来存储输入的字符
    printf("请输入多个字符:");
    scanf("%s", str); // 使用%s格式符来读取多个字符,直到遇到空格或换行符为止
    printf("您输入的字符是:%s\n", str);
    
    return 0;
}

In the code above, we defined a sufficiently large character array ‘str’ to store multiple input characters. We then used the ‘scanf’ function with the ‘%s’ format specifier to read the input characters until a space or newline was encountered. Finally, we printed the input characters.

bannerAds