首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解问题-真0不是数字0

理解问题-真0不是数字0
EN

Stack Overflow用户
提问于 2014-02-16 14:37:45
回答 3查看 80关注 0票数 1

目前,我正在阅读一本C++教学书,但我不理解以下内容:

作者对\0说,这是真正的0,而不是数字0。

所以,也许任何人都可以澄清真实0和数字0之间的区别是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-16 14:40:03

\0用作字符串终止字符。不要与字符0混淆,该字符的ascii值为48。因此,当迭代以空结尾的C样式字符串时,当您将该字符与0进行比较时,\0将生成true,而不是0

票数 4
EN

Stack Overflow用户

发布于 2014-02-16 14:57:11

这句话

作者对0说,这是真正的0,而不是数字0。

看上去模棱两可。

此外,我认为有一个错误。而不是\0,应该有'\0'

我认为作者指的是人物文字。例如,' 0‘是一个字符文字,表示数字0,并具有与0不同的内部表示形式。例如,对于ASCII代码'0‘有内部表示形式48或0x30 (以十六进制表示)。对于EBCDIC,“0”具有内部表示形式0xF0。

字符“\ 0”的内部表示形式确实为0。'\0‘是一个叫octal escape character literal的词.例如,'\010‘是十进制数8的八进制表示。

由于char类型属于算术类型,那么当'\0‘在某些算术表达式中使用时,它确实有值0,而'0’则具有值0x30 (用于ASCII),或0xF0 (用于EBCDIC)或其他值,这取决于已使用平台中的符号编码系统。

票数 1
EN

Stack Overflow用户

发布于 2014-02-16 14:40:22

\0是C++中的空字符,主要用于终止字符串等。

其中,0是一个数字数字。

很清楚地解释了常见问题,

代码语言:javascript
复制
C programmers must understand that NULL and 0 are interchangeable in pointer 
contexts, and that an uncast 0 is perfectly acceptable. Any usage of NULL (as 
opposed to 0) should be considered a gentle reminder that a pointer is involved;
programmers should not depend on it (either for their own understanding or the
compiler's) for distinguishing pointer 0's from integer 0's.

It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be 
used when another kind of 0 is required, even though it might work, because doing
so sends the wrong stylistic message. (Furthermore, ANSI allows the definition of
NULL to be ((void *)0), which will not work at all in non-pointer contexts.) In
particular, do not use NULL when the ASCII null character (NUL) is desired. 
Provide your own definition

#define NUL '\0'

if you must. 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21812571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档