Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When terminating a string, it seems to me that logically char c=0 is equivalent to char c='', since the "null" (ASCII 0) byte is 0, but usually people tend to do '' instead. Is this purely out of preference or should it be a better "practice"?

What is the preferred choice?


EDIT: K&R says: "The character constant '' represents the character with value zero, the null character. '' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
426 views
Welcome To Ask or Share your Answers For Others

1 Answer

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     ?       ^@                          Null character

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...