温馨提示×

c语言如何输入字符串

小亿
83
2023-12-22 05:36:50
栏目: 编程语言

在C语言中,可以使用scanf函数或gets函数来输入字符串。

  1. 使用scanf函数:

    #include <stdio.h> int main() { char str[100]; printf("请输入字符串:"); scanf("%s", str); printf("您输入的字符串是:%s\n", str); return 0; } 
  2. 使用gets函数:

    #include <stdio.h> int main() { char str[100]; printf("请输入字符串:"); gets(str); printf("您输入的字符串是:%s\n", str); return 0; } 

注意:使用gets函数时要注意字符串的长度,避免溢出。推荐使用fgets函数来输入字符串,可以指定字符串的最大长度,避免溢出问题。

0