What happened when we do not include '\0' at the end of string in C?

后端 未结 5 2006
醉话见心
醉话见心 2020-12-10 23:09

In C, when I initialize my array this way:

char full_name[] = {
    \'t\', \'o\', \'a\', \'n\'
};

and print it with printf(\"%s\", fu

5条回答
  •  孤城傲影
    2020-12-10 23:53

    If you use a non-null-terminated char sequence as a string, C functions will just keep going. It's the '\0' that tells them to stop. So, whatever happens to be in memory after the sequence will be taken as part of the string. This may eventually cross a memory boundary and cause an error, or it may just print gibberish if it happens to find a '\0' somewhere and stop.

提交回复
热议问题