Execution of printf() and Segmentation Fault

后端 未结 4 647
清酒与你
清酒与你 2020-11-30 07:59
#include

int main()
{
    char *name = \"Vikram\";
    printf(\"%s\",name);
    name[1]=\'s\';
    printf(\"%s\",name);
    return 0;
}
4条回答
  •  萌比男神i
    2020-11-30 08:07

    By default when stdout is connected to a terminal, the stream is line-buffered. In practice, in your example the absence of '\n' (or of an explicit stream flush) is why you don't get the characters printed.

    But in theory undefined behavior is not bounded (from the Standard "behavior [...] for which this International Standard imposes no requirements") and the segfault can happen even before the undefined behavior occurs, for example before the first printf call!

提交回复
热议问题