如果SCANF接受比缓冲区中的空间更多的角色会发生什么?
如果SCANF接受比缓冲区中的空间更多的角色会发生什么? 例如,
char foo[2], bar[5]="abcd";
sscanf(bar, "%s", foo);
what will happen if scanf accepts more characters than there is room for in the buffer?
For example,
char foo[2], bar[5]="abcd";
sscanf(bar, "%s", foo);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将是命令中提到的@mad物理学家的不确定行为来自缓冲区的确切尺寸。
读取比缓冲区的大小少1个,以留出空间的空字符。
示例:
That will be an undefined behavior as @Mad Physicist mentioned in the command but if you want to avoid I, you can use the
width
modifier ofscanf()
function to read the exact size from the buffer.Read by 1 less than the size of the buffer to leave space for the the null character.
The example: