why printf works on non-terminated string

后端 未结 6 1909
渐次进展
渐次进展 2021-01-05 15:47

I am wondering how does printf() figure out when to stop printing a string, even I haven\'t put a termination character at the end of the string? I did an experiment by mall

6条回答
  •  迷失自我
    2021-01-05 16:15

    Assuming you really did malloc 10 characters, and you really did set each and every character with a value other than null ('\0'), then where is the guarantee that the character you didn't allocate which immediately followed in memory wasn't null by chance?

    You may have used one of a number of function calls that are smart enough to set the last character as null, even if you passed it enough information to possibly "set" is to be non-null, but with so few details, we will never know.

提交回复
热议问题