String termination - char c=0 vs char c='\\0'

对着背影说爱祢 提交于 2019-11-27 07:04:06

http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart

Binary   Oct  Dec    Hex    Abbr    Unicode  Control char  C Escape code   Name
0000000  000  0      00     NUL     ␀       ^@            \0              Null character

There's no difference, but the more idiomatic one is '\0'.

Putting it down as char c = 0; could mean that you intend to use it as a number (e.g. a counter). '\0' is unambiguous.

Michal Bukovy

'\0' is just an ASCII character. The same as 'A', or '0' or '\n'
If you write char c = '\0', it's the same aschar c = 0;
If you write char c = 'A', it's the same as char c = 65

It's just a character representation and it's a good practice to write it, when you really mean the NULL byte of string. Since char is in C one byte (integral type), it doesn't have any special meaning.

Preferred choice is that which can give people reading your code an ability to understand how do you use your variable - as a number or as a character. Best practice is to use 0 when you mean you variable as a number and to use '\0' when you mean your variable is a character.

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