关于\0和0的问题

c415768667 2013-01-24 04:46:24
请问\0和0有什么区别有什么联系
...全文
570 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazyboysk 2014-10-15
  • 打赏
  • 举报
回复
32位机器字符串中 0是1字节的char,其编码为48x,测试printf("%d\n",'0');输出48 \0是占据1字节的0,测试printf("%x\n",'\0');测一下是0,也就是他是内存中的纯0,不是字符0 用memset可以将字符串空间内存清0,即将空间全用16进制0填充,这里的清0就做了字符串结尾\0的作用 char s1[10]; memset(s1,0,10);//按字节清0,将每一字节都标识为00x s1[0]='a'; printf("%s\n",s1);//a printf("%x\n",s1[9]);//0 也就是说所谓的\0就是十六进制的0,系统遇到他就知道串结束了 其实这么写就是为了区别告诉计算机0是一个字符,而\0结尾是内存为0即串结束
wuchangdao 2013-10-19
  • 打赏
  • 举报
回复
int 0 按字符打开就是/0
深_SHEN 2013-01-25
  • 打赏
  • 举报
回复
在ASCII下,\0就是占用一个字节的数0,用于字符串结尾。在UNICODE也是数0,但是占用两个字节。明白了吗?
wizard_tiger 2013-01-25
  • 打赏
  • 举报
回复
'\0'表示一个字符,这个字符的ASCII码为0。 而这个字符是一个不可显示的字符为字符串的结尾。
I'm Daniel Du 2013-01-25
  • 打赏
  • 举报
回复
引用 8 楼 gumh 的回复:
'\0' == 0 主要是爲了與'0'區別開來,所以要用轉義符號 \ '0' != '\0'
+1
yu275184637 2013-01-25
  • 打赏
  • 举报
回复
ascii 0 和 十进制 0
ForestDB 2013-01-25
  • 打赏
  • 举报
回复
内容一样,逻辑意义不一样 \0被认为是char 0是int
漫步者、 2013-01-24
  • 打赏
  • 举报
回复
请问\0和0有什么区别有什么联系 0,可以使数字0,可以是空白字符的ASCII \0,一个转义字符,一般作为字符串输出的结束符!
阿麦 2013-01-24
  • 打赏
  • 举报
回复
类似的还有 0 NUL NULL 的差别
prajna 2013-01-24
  • 打赏
  • 举报
回复
'\0' == 0 主要是爲了與'0'區別開來,所以要用轉義符號 \ '0' != '\0'
zeko075 2013-01-24
  • 打赏
  • 举报
回复
其实通用···'\0'的值也就是0,只是写的时候还是习惯在字符串末尾加'\0'但是手动加0也行···
AnYidan 2013-01-24
  • 打赏
  • 举报
回复
引用 2 楼 txg703003659 的回复:
一样的吧,\0在内存中也是0
++
Joseph_ 2013-01-24
  • 打赏
  • 举报
回复
引用 楼主 c415768667 的回复:
请问\0和0有什么区别有什么联系
\0是一个字符 一般表示字符串结尾 0呢就是0吧
__YY__ 2013-01-24
  • 打赏
  • 举报
回复
都是0,具体差别不清楚耶! 我通常用0是用来赋数组初值,'\0'则是用来赋值一个字符,充当结束符。 ‘\0’只是一个字符,而0 的用途有点多额 0 可以代表‘\0’ 以上仅是个人观点,坐等其他回答...
赵4老师 2013-01-24
  • 打赏
  • 举报
回复
C++ Character Constants Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes. Microsoft Specific For Microsoft C++, the source and execution character sets are both ASCII. END Microsoft Specific There are three kinds of character constants: Normal character constants Multicharacter constants Wide-character constants Note Use wide-character constants in place of multicharacter constants to ensure portability. Character constants are specified as one or more characters enclosed in single quotation marks. For example: char ch = 'x'; // Specify normal character constant. int mbch = 'ab'; // Specify system-dependent // multicharacter constant. wchar_t wcch = L'ab'; // Specify wide-character constant. Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message. Syntax character-constant : 'c-char-sequence' L'c-char-sequence' c-char-sequence : c-char c-char-sequence c-char c-char : any member of the source character set except the single quotation mark ('), backslash (\), or newline character escape-sequence escape-sequence : simple-escape-sequence octal-escape-sequence hexadecimal-escape-sequence simple-escape-sequence : one of \' \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence : \octal-digit \octal-digit octal-digit \octal-digit octal-digit octal-digit hexadecimal-escape-sequence : \xhexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.) The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example: char schar = 'x'; // Normal character constant wchar_t wchar = L'\x81\x19'; // Wide-character constant Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences. Table 1.2 C++ Reserved or Nongraphic Characters Character ASCII Representation ASCII Value Escape Sequence Newline NL (LF) 10 or 0x0a \n Horizontal tab HT 9 \t Vertical tab VT 11 or 0x0b \v Backspace BS 8 \b Carriage return CR 13 or 0x0d \r Formfeed FF 12 or 0x0c \f Alert BEL 7 \a Backslash \ 92 or 0x5c \\ Question mark ? 63 or 0x3f \? Single quotation mark ' 39 or 0x27 \' Double quotation mark " 34 or 0x22 \" Octal number ooo — \ooo Hexadecimal number hhh — \xhhh Null character NUL 0 \0 If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued. Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence. Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example: wchar_t och = L'\076a'; // Sequence terminates at a char ch = '\233'; // Sequence terminates after 3 characters Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit. Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)
  • 打赏
  • 举报
回复
一样的吧,\0在内存中也是0
lee_鹿游原 2013-01-24
  • 打赏
  • 举报
回复
\0是C++中字符串的结尾标志,存储在字符串的结尾。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧