C Language: Input Strings Using Char Arrays

In the C language, char arrays are used to store and manipulate strings. You can input strings using the scanf function, or use the gets or fgets function.

  1. Input a string using the scanf function.
char str[100];
printf("请输入字符串:");
scanf("%s", str);
printf("您输入的字符串是:%s\n", str);
  1. Input a string using the gets function.
char str[100];
printf("请输入字符串:");
gets(str);
printf("您输入的字符串是:%s\n", str);
  1. Input a string using the fgets function.
char str[100];
printf("请输入字符串:");
fgets(str, 100, stdin);
printf("您输入的字符串是:%s\n", str);

It is worth noting that the function “gets” has security issues and is not recommended for use. Instead, it is advised to use the function “fgets” to input strings, where you can specify the buffer size to prevent buffer overflow.

bannerAds