0 vs '\0'

D

DevarajA

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?
 
R

Richard Tobin

DevarajA said:
Are 0 and '\0' the same to terminate a string?

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
 
F

Flash Gordon

DevarajA said:
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?

Yes, although \0 is the only one for which C provides a specific meaning.
 
M

Mabden

Richard Tobin said:
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.

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.
 
D

DevarajA

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

Obviously I wasn't talking about this because I said 0, not '0' :)
 
L

Leonardo Andrade

Yes, I read once that '\0' is more elegant. And the most programmer
using '\0' for terminate strings.
Besides, my compiler no accept 0 to equal or terminates string (only
numeric values).

Greetings.
 
C

Christopher Benson-Manica

Provide. Context. This is a broken record. Look in the archives of
this group for any of the ULONG_MAX posts describing how to get Google
to provide proper context when replying to posts.
Yes, I read once that '\0' is more elegant. And the most programmer
using '\0' for terminate strings.

For the record, we do not, and I personally prefer unadorned 0 for
terminating strings.
Besides, my compiler no accept 0 to equal or terminates string (only
numeric values).

Sounds like your compiler is broken.
 
E

Emmanuel Delahaye

DevarajA wrote on 12/09/05 :
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?

'\0' is the octal value of 0. They are identical.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
K

Keith Thompson

Christopher Benson-Manica said:
Provide. Context. This is a broken record. Look in the archives of
this group for any of the ULONG_MAX posts describing how to get Google
to provide proper context when replying to posts.


For the record, we do not, and I personally prefer unadorned 0 for
terminating strings.
Why?


Sounds like your compiler is broken.

Or he's describing it incorrectly.
 
C

Christopher Benson-Manica


Now that I think about it, I'm not sure, since I definitely prefer NULL
to unadorned 0 for pointers. I suppose it's just the company style
growing on me, i.e., habit.
 
J

Jack Klein

DevarajA wrote on 12/09/05 :

'\0' is the octal value of 0. They are identical.

But the character literal '0' appearing unadorned in C source is also
an octal constant expression.
 
K

Keith Thompson

Jack Klein said:
But the character literal '0' appearing unadorned in C source is also
an octal constant expression.

You mean the integer literal 0, not the character literal '0'.
Or rather, to be even pickier, integer constant and character
constant, respectively.
 
A

Anand

Emmanuel said:
DevarajA wrote on 12/09/05 :



'\0' is the octal value of 0. They are identical.
<OT-ish purpose="academic-intrests" nonsensical="99.99999%">
So just out of curiosity the *word* octal you prefferred instead of
_"integer-value-constant"_ because of the fact that it starts with 0?
[ I know 0 is same in bin, oct, dec ,hex etc..]
So does the compiler would start off processing the token as octal the
moment it starts off with 0? [ is so all 0's in C are actually 'octal' ]

</OT-ish>
 
A

Anand

Emmanuel said:
DevarajA wrote on 12/09/05 :



'\0' is the octal value of 0. They are identical.
<OT purpose="academic-intrests|curiosity" nonsensical="99.99999%">

Did you preffer the word *octal* instead of *"integer-value-constant"*
because of the fact that it starts with 0?
[ I know 0 is same in bin, oct, dec ,hex etc..]

So does the compiler would start off processing the token as octal the
moment it starts off with 0?
</OT>
 
K

Keith Thompson

Anand said:
Emmanuel said:
DevarajA wrote on 12/09/05 :

'\0' is the octal value of 0. They are identical.
<OT purpose="academic-intrests|curiosity" nonsensical="99.99999%">

Did you preffer the word *octal* instead of *"integer-value-constant"*
because of the fact that it starts with 0?
[ I know 0 is same in bin, oct, dec ,hex etc..]

So does the compiler would start off processing the token as octal the
moment it starts off with 0?
</OT>

Yes (and no, it's not really off-topic).

The grammar for the various kinds of integer constants, octal,
decimal, and hexadecimal, makes it clear that an octal constant always
starts with '0', a decimal constant always starts with a non-zero
digit, and a hexadecimal constant always starts with "0x" or "0X".
0 is an octal constant.
 
I

Irrwahn Grausewitz

Keith Thompson said:
Anand <[email protected]> writes:
<OT purpose="academic-intrests|curiosity" nonsensical="99.99999%">

Did you preffer the word *octal* instead of *"integer-value-constant"*
because of the fact that it starts with 0?
[ I know 0 is same in bin, oct, dec ,hex etc..]

So does the compiler would start off processing the token as octal the
moment it starts off with 0?
</OT>

Yes (and no, it's not really off-topic).

The grammar for the various kinds of integer constants, octal,
decimal, and hexadecimal, makes it clear that an octal constant always
starts with '0', a decimal constant always starts with a non-zero
digit, and a hexadecimal constant always starts with "0x" or "0X".
0 is an octal constant.

Just to add, that the hexadecimal escape sequence notation for
character constants is '/xHEXDIGITS'.

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top