0 vs'\0' [英] 0 vs '\0'

查看:78
本文介绍了0 vs'\0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0和''\'''是否相同以终止字符串?或''\ 0''更便携?

使用数字制作的AFAIK转义序列应该将该值分配给

相对字符。这是真的吗?


-

Devaraja(Xdevaraja87 ^ gmail ^ c0mX)

Linux注册用户#338167
http://counter.li.org

Are 0 and ''\0'' the same to terminate a string? Or ''\0'' is more portable?
AFAIK escape sequences made with numbers should assign that value to the
relative char. Is that true?

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org

推荐答案

在文章< fx ********************* @ news4.tin.it>,

DevarajA< no@spam.com>写道:
In article <fx*********************@news4.tin.it>,
DevarajA <no@spam.com> wrote:
0和''\'''相同,以终止字符串?
Are 0 and ''\0'' the same to terminate a string?




是的,和它们也用于其他目的。它们都是
代表整数零。


选择''\0''的唯一理由是,如果你认为它会让它更清晰

给某人读取它被用作角色的代码。


- Richard



Yes, and they are the same for other purposes too. They both
represent the integer zero.

The only reason to prefer ''\0'' is if you think it will make it clearer
to someone reading the code that it is being used as a character.

-- Richard


DevarajA写道:
DevarajA wrote:
0和''\ 0''相同以终止字符串?或''\ 0''更便携?


他们是相同的,直到两者都是int类型,所以它纯粹是

a样式。

AFAIK逃脱用数字创建的序列应该将该值赋给
相对字符。这是真的吗?
Are 0 and ''\0'' the same to terminate a string? Or ''\0'' is more portable?
They are identical right down to both being of type int, so it is purely
a matter of style.
AFAIK escape sequences made with numbers should assign that value to the
relative char. Is that true?




是的,虽然\ 0是C提供特定含义的唯一一个。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但这是真的,我读了它。



Yes, although \0 is the only one for which C provides a specific meaning.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


" Richard Tobin" < RI ***** @ cogsci.ed.ac.uk>在消息中写道

news:dg ********** @ pc-news.cogsci.ed.ac.uk ...
"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:dg**********@pc-news.cogsci.ed.ac.uk...
在文章< ; fx ********************* @ news4.tin.it>,
DevarajA< no@spam.com>写道:
In article <fx*********************@news4.tin.it>,
DevarajA <no@spam.com> wrote:
0和''\'''相同以终止字符串?
Are 0 and ''\0'' the same to terminate a string?



是的,它们是相同的用于其他目的。它们都代表整数零。

选择''\0''的唯一理由是,如果你认为它会让那些阅读代码的人更清楚它被用作角色。



Yes, and they are the same for other purposes too. They both
represent the integer zero.

The only reason to prefer ''\0'' is if you think it will make it clearer
to someone reading the code that it is being used as a character.




嗯,还有一个问题是在

中是否使用0或\0字符串。


char str1 [] ="结束为零... 0" ;;

char str2 [] ="结束于此... \ 0" ;;


字符串str1是19个字符,最后两个字符是ASCII零(0x30,

IIRC)和char zero ; (0×00)。 strlen()将报告18.

字符串str2是14个字符,带有2个char zeros在末尾。 strlen()将

报告12.


-

Mabden



Well, there is also the issue of whether 0 or \0 is used inside the
string.

char str1[]="Ends with zero...0";
char str2[]="Ends here...\0";

String str1 is 19 chars and the last two chars are an ASCII zero (0x30,
IIRC) and a "char zero" (0x00). strlen() would report 18.
String str2 is 14 chars with 2 "char zeros" at the end. strlen() would
report 12.

--
Mabden


这篇关于0 vs'\0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆