C语言:获取控制台输入:gets()

说明

更加简单,可以替代 scanf()

原型:

# include <stdio.h>

char *gets(char *str);

缺点

由于 gets() 不检查字符串string的大小,必须遇到换行符或文件结尾才会结束输入,因此容易造成缓存溢出的安全性问题,导致程序崩溃,可以使用 fgets() 代替

例子

# include <stdio.h>
int main(void)
{
    char str[20] = "\0";  //字符数组初始化\0
    printf("请输入字符串:");
    gets(str);
    printf("%s\n", str);
    return 0;
}

参考:
http://c.biancheng.net/view/233.html


原文出处:http://malaoshi.top/show_1IX3TPnWswN6.html