How to input a string in C language?

In C language, you can use the scanf function to input strings. Below is a simple example:

#include <stdio.h>

int main() {
    char str[100];  // 声明一个足够大的字符数组来存储字符串

    printf("请输入一个字符串:");
    scanf("%s", str);  // 使用%s格式说明符读入字符串

    printf("你输入的字符串是:%s\n", str);

    return 0;
}

In the example above, the scanf function uses the %s format specifier to input a string. This specifier indicates reading a continuous sequence of non-empty characters, ending when a space or newline is encountered. The scanf function reads a string from the standard input stream and stores it in an array called str. Finally, the printf function is used to output the inputted string to the screen.

bannerAds