关于gets的返回值问题

Jerryup 2014-04-14 08:10:20
在c prime plus中,经常遇到类似char a[100];
while(gets(a)!=NULL)
{
..............
}
并解释当输入的字符数超过100时候,gets出错并返回NULL,但是经本人测试,即使超出a的长度,在vc6.0和vs2010中均能打印出。没有报错,有知道能否解释一下。谢谢!
...全文
690 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jerryup 2014-04-14
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
[quote=引用 1 楼 zjq9931 的回复:] char a[100]; 取超过100个,是溢出了,在vc6.0和vs2010没有报错,但程序大了之后就不好说了,指不定出什么烂七八糟的问题呢。。。 这种情况很危险。。。 所以可以用:fgets(a, 99, stdin);// 这个来代替。只读取99个,末尾留上结束位。
纠错: char a[100]; 取超过100个,是溢出了,在vc6.0和vs2010没有报错,但程序大了之后就不好说了,指不定出什么烂七八糟的问题呢。。。 这种情况很危险。。。 所以可以用:fgets(a, 100, stdin);// 这个来代替。只读取99个,末尾留上结束位。 [/quote]好细心,呵呵。
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
gets, _getws Get a line from the stdin stream. char *gets( char *buffer ); wchar_t *_getws( wchar_t *buffer ); Routine Required Header Compatibility gets <stdio.h> ANSI, Win 95, Win NT _getws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred. Parameter buffer Storage location for input string Remarks The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _getts gets gets _getws Example /* GETS.C */ #include <stdio.h> void main( void ) { char line[81]; printf( "Input a string: " ); gets( line ); printf( "The line entered was: %s\n", line ); } Output Input a string: Hello! The line entered was: Hello! Stream I/O Routines See Also fgets, fputs, puts
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
引用 1 楼 zjq9931 的回复:
char a[100]; 取超过100个,是溢出了,在vc6.0和vs2010没有报错,但程序大了之后就不好说了,指不定出什么烂七八糟的问题呢。。。 这种情况很危险。。。 所以可以用:fgets(a, 99, stdin);// 这个来代替。只读取99个,末尾留上结束位。
纠错: char a[100]; 取超过100个,是溢出了,在vc6.0和vs2010没有报错,但程序大了之后就不好说了,指不定出什么烂七八糟的问题呢。。。 这种情况很危险。。。 所以可以用:fgets(a, 100, stdin);// 这个来代替。只读取99个,末尾留上结束位。
  • 打赏
  • 举报
回复
啊 这个问题没想过,学习了。
  • 打赏
  • 举报
回复
char a[100]; 取超过100个,是溢出了,在vc6.0和vs2010没有报错,但程序大了之后就不好说了,指不定出什么烂七八糟的问题呢。。。 这种情况很危险。。。 所以可以用:fgets(a, 99, stdin);// 这个来代替。只读取99个,末尾留上结束位。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧