如果SCANF接受比缓冲区中的空间更多的角色会发生什么?

发布于 2025-02-10 07:50:04 字数 116 浏览 3 评论 0原文

如果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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-02-17 07:50:04

这将是命令中提到的@mad物理学家的不确定行为来自缓冲区的确切尺寸。

读取比缓冲区的大小少1个,以留出空间的空字符。

示例:

char buff[8], str[32]="asdfasdfasasdfasdfdfasdf";
sscanf(buff, "%7s", str);

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 of scanf() 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:

char buff[8], str[32]="asdfasdfasasdfasdfdfasdf";
sscanf(buff, "%7s", str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文