Is scanf(“%4s%4s”, b, b); well-defined? [duplicate]

旧时模样 提交于 2020-05-14 18:42:05

问题


#include <stdio.h>

int main(void) 
{
    char b[5];
    scanf("%4s%4s", b, b);
    //My input: "qwer<Enter>sgsh<Enter>"
    printf("%s", b);
    //Output: sgsh
}

C99: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

In this case, I am modifying the value of b twice. Isn't it undefined behavior?


回答1:


From this scanf reference:

There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.

So what you're doing is defined and should work well.



来源:https://stackoverflow.com/questions/61650409/is-scanf4s4s-b-b-well-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!