Confused between 0 and '\0' 
Author Message
 Confused between 0 and '\0'

Well I have searched the docs, but these are the things that I am best
able to find
1)         '\0' has a representation of all zeros
2)          Strings are terminated by '\0'

But is 0 gurantteed to have a have an all bit zero representation.
Further does that make this

char string[10];

string[0] = 0;

Also if we say that all bits zero is for null char then if we have

unsigned char c[10] ;

c = '\0'

Would it still mean all bits would be guranteed to be null,
specifically is it null character or null unsigned char.



Thu, 30 Sep 2004 11:03:44 GMT  
 Confused between 0 and '\0'


Quote:
> Well I have searched the docs, but these are the things that I am best
> able to find
> 1)         '\0' has a representation of all zeros
> 2)          Strings are terminated by '\0'

> But is 0 gurantteed to have a have an all bit zero representation.
> Further does that make this

> char string[10];

> string[0] = 0;

> Also if we say that all bits zero is for null char then if we have

> unsigned char c[10] ;

> c = '\0'

> Would it still mean all bits would be guranteed to be null,
> specifically is it null character or null unsigned char.

I believe it's more a matter of style to use '\0' (zero) when dealing with
char arrays or strings and 0 (zero) for things not necessarily strings. It's
zero either way.


Thu, 30 Sep 2004 11:06:56 GMT  
 Confused between 0 and '\0'

Quote:

> Well I have searched the docs, but these are the things that I am best
> able to find
> 1)         '\0' has a representation of all zeros

'\0' is merely an `int' constant with value zero.  It is not
necessarily all-bits-zero.

Quote:
> 2)          Strings are terminated by '\0'

> But is 0 gurantteed to have a have an all bit zero representation.

Not any more than '\0'.  '\0' and 0 have exactly the same
meaning, type, and value.

Quote:
> Further does that make this

> char string[10];

> string[0] = 0;

That's fine and creates a string of length zero.  Writing 0 is
equivalent to writing '\0'.

Quote:
> Also if we say that all bits zero is for null char then if we have

Bad assumption, so the question doesn't make sense.
--
"I've been on the wagon now for more than a decade. Not a single goto
 in all that time. I just don't need them any more. I don't even use
 break or continue now, except on social occasions of course. And I
 don't get carried away." --Richard Heathfield


Thu, 30 Sep 2004 11:06:24 GMT  
 Confused between 0 and '\0'



Quote:


> > Well I have searched the docs, but these are the things that I am best
> > able to find
> > 1)         '\0' has a representation of all zeros
> > 2)          Strings are terminated by '\0'

> > But is 0 gurantteed to have a have an all bit zero representation.
> > Further does that make this

> > char string[10];

> > string[0] = 0;

> > Also if we say that all bits zero is for null char then if we have

> > unsigned char c[10] ;

> > c = '\0'

> > Would it still mean all bits would be guranteed to be null,
> > specifically is it null character or null unsigned char.

> I believe it's more a matter of style to use '\0' (zero) when dealing with
> char arrays or strings and 0 (zero) for things not necessarily strings.
It's
> zero either way.

THe distinction appears with character or string literals.

'\0' has a 'native' value of zero (all bits zero).
'0' does not.

-Mike

- Show quoted text -



Thu, 30 Sep 2004 11:42:46 GMT  
 Confused between 0 and '\0'

Quote:

> THe distinction appears with character or string literals.

> '\0' has a 'native' value of zero (all bits zero).

All value bits are zero. If the int type has any padding bits, then the
padding bits need not be zero.

--
Simon.



Thu, 30 Sep 2004 12:34:00 GMT  
 Confused between 0 and '\0'


Quote:


> > THe distinction appears with character or string literals.

> > '\0' has a 'native' value of zero (all bits zero).

> All value bits are zero. If the int type has any padding bits, then the
> padding bits need not be zero.

   ISO/IEC 14882:1998(E)

   3 Basic concepts

   3.9.1 Fundamental types

1  Objects declared as characters (char) shall be large enough to
   store any member of the implementation's basic character set.
   If a character from this set is stored in a character object,
   the integral value of that character object is equal to the
   value of the single character literal form of that character.
   It is implementation-defined whether a char object can hold
   negative values. Characters can be explicitly declared unsigned
   or signed. Plain char, signed char, and unsigned char are three
   distinct types. A char, a signed char, and an unsigned  char
   occupy the same amount of storage and have the same alignment
   requirements (3.9); that is, they have the same object
   representation. For character types, all bits of the object <<<===LOOK===
   representation participate in the value representation. For
   unsigned character types, all possible bit patterns of the
   value representation represent numbers. These requirements do
   not hold for other types. In any particular implementation,
   a plain char object can take on either the same values as a
   signed char or an unsigned char; which one is implementation-
   defined.

ISO/IEC 9899 (C standard) has similar language, but not as
'straightforward' as this.

-Mike



Thu, 30 Sep 2004 13:20:00 GMT  
 Confused between 0 and '\0'

Quote:




> > > THe distinction appears with character or string literals.

> > > '\0' has a 'native' value of zero (all bits zero).

> > All value bits are zero. If the int type has any padding bits, then the
> > padding bits need not be zero.

>    ISO/IEC 14882:1998(E)

What? Ah, that's C++. It's quite a different story, because of course '\0'
has type char in C++, but type int in C.

Quote:
>    3 Basic concepts

>    3.9.1 Fundamental types

> 1  Objects declared as characters (char) shall be large enough to
>    store any member of the implementation's basic character set.
>    If a character from this set is stored in a character object,
>    the integral value of that character object is equal to the
>    value of the single character literal form of that character.
>    It is implementation-defined whether a char object can hold
>    negative values. Characters can be explicitly declared unsigned
>    or signed. Plain char, signed char, and unsigned char are three
>    distinct types. A char, a signed char, and an unsigned  char
>    occupy the same amount of storage and have the same alignment
>    requirements (3.9); that is, they have the same object
>    representation. For character types, all bits of the object
<<<===LOOK===
>    representation participate in the value representation. For
>    unsigned character types, all possible bit patterns of the
>    value representation represent numbers. These requirements do
>    not hold for other types. In any particular implementation,
>    a plain char object can take on either the same values as a
>    signed char or an unsigned char; which one is implementation-
>    defined.

There is no such requirement on the int type, which is the type of '\0' in
C.

Quote:
> ISO/IEC 9899 (C standard) has similar language, but not as
> 'straightforward' as this.

No, it doesn't apply in C.

--
Simon.



Thu, 30 Sep 2004 13:08:08 GMT  
 Confused between 0 and '\0'

Quote:




> > > THe distinction appears with character or string literals.

> > > '\0' has a 'native' value of zero (all bits zero).

> > All value bits are zero. If the int type has any padding bits, then the
> > padding bits need not be zero.

>    ISO/IEC 14882:1998(E)

>    3 Basic concepts

>    3.9.1 Fundamental types

> 1  Objects declared as characters (char) shall be large enough to
>    store any member of the implementation's basic character set.
>    If a character from this set is stored in a character object,
>    the integral value of that character object is equal to the
>    value of the single character literal form of that character.
>    It is implementation-defined whether a char object can hold
>    negative values. Characters can be explicitly declared unsigned
>    or signed. Plain char, signed char, and unsigned char are three
>    distinct types. A char, a signed char, and an unsigned  char
>    occupy the same amount of storage and have the same alignment
>    requirements (3.9); that is, they have the same object
>    representation. For character types, all bits of the object <<<===LOOK===
>    representation participate in the value representation. For
>    unsigned character types, all possible bit patterns of the
>    value representation represent numbers. These requirements do
>    not hold for other types. In any particular implementation,
>    a plain char object can take on either the same values as a
>    signed char or an unsigned char; which one is implementation-
>    defined.

> ISO/IEC 9899 (C standard) has similar language, but not as
> 'straightforward' as this.

*sigh*  This is one of the reasons why it's a very bad idea to
crosspost questions between comp.lang.c and comp.lang.c++.  This
answer to this question is different in C and in C++.

Mike is right in C++, because in C++ an ordinary character
constant that contains a single char has type `char'.  Since
character types don't have padding in C++, '\0' thus has all bits
zero.

Simon is right in C, because in C an ordinary single-character
character constant has type `int'.  Type `int' may have padding
bits in C, thus '\0' may not have all bits zero in C.

Can we stop the arguing now?
--
"I hope, some day, to learn to read.
 It seems to be even harder than writing."
--Richard Heathfield



Thu, 30 Sep 2004 13:19:44 GMT  
 Confused between 0 and '\0'

Quote:




> > > THe distinction appears with character or string literals.

> > > '\0' has a 'native' value of zero (all bits zero).

> > All value bits are zero. If the int type has any padding bits, then the
> > padding bits need not be zero.

>    ISO/IEC 14882:1998(E)

>    3 Basic concepts

>    3.9.1 Fundamental types

<snip>

Quote:
>    representation. For character types, all bits of the object <<<===LOOK===
>    representation participate in the value representation. For
>    unsigned character types, all possible bit patterns of the
>    value representation represent numbers. These requirements do

<snip>

Quote:
> ISO/IEC 9899 (C standard) has similar language, but not as
> 'straightforward' as this.

That's all well and good, but the fact is, you're still quoting the
wrong standard (and the wrong section).  In C, '\0' is *not* a char
type.  Also, in C, there is semantically *no* difference between '\0'
and 0.  They are completely equivalent in every way.

-Micah

P.S., I didn't realize this was crossposted, and am currently reading
in comp.lang.c.  Of course, all comments above are true only for C.



Thu, 30 Sep 2004 14:27:53 GMT  
 Confused between 0 and '\0'

Quote:




> > > > THe distinction appears with character or string literals.

> > > > '\0' has a 'native' value of zero (all bits zero).

> > > All value bits are zero. If the int type has any padding bits, then
the
> > > padding bits need not be zero.

> >    ISO/IEC 14882:1998(E)

> >    3 Basic concepts

> >    3.9.1 Fundamental types

> > 1  Objects declared as characters (char) shall be large enough to
> >    store any member of the implementation's basic character set.
> >    If a character from this set is stored in a character object,
> >    the integral value of that character object is equal to the
> >    value of the single character literal form of that character.
> >    It is implementation-defined whether a char object can hold
> >    negative values. Characters can be explicitly declared unsigned
> >    or signed. Plain char, signed char, and unsigned char are three
> >    distinct types. A char, a signed char, and an unsigned  char
> >    occupy the same amount of storage and have the same alignment
> >    requirements (3.9); that is, they have the same object
> >    representation. For character types, all bits of the object
<<<===LOOK===
> >    representation participate in the value representation. For
> >    unsigned character types, all possible bit patterns of the
> >    value representation represent numbers. These requirements do
> >    not hold for other types. In any particular implementation,
> >    a plain char object can take on either the same values as a
> >    signed char or an unsigned char; which one is implementation-
> >    defined.

> > ISO/IEC 9899 (C standard) has similar language, but not as
> > 'straightforward' as this.

> *sigh*  This is one of the reasons why it's a very bad idea to
> crosspost questions between comp.lang.c and comp.lang.c++.  This
> answer to this question is different in C and in C++.

> Mike is right in C++, because in C++ an ordinary character
> constant that contains a single char has type `char'.  Since
> character types don't have padding in C++, '\0' thus has all bits
> zero.

> Simon is right in C, because in C an ordinary single-character
> character constant has type `int'.  Type `int' may have padding
> bits in C, thus '\0' may not have all bits zero in C.

Oh s**t what have I gone and said? :-)  This is my
'oops of the month' I think. :-)

Quote:
> Can we stop the arguing now?

Well, I've utterly and completely (and correctly) lost this
'argument', so sure I'll stop. :-)

(abashed)

-Mike



Thu, 30 Sep 2004 15:17:33 GMT  
 Confused between 0 and '\0'

Quote:


> > '\0' has a 'native' value of zero (all bits zero).

> All value bits are zero. If the int type has any padding
> bits, then the padding bits need not be zero.

AFAIK, the C-committee decided padding bits are no longer
allowed in integer types!

willem



Thu, 30 Sep 2004 17:10:18 GMT  
 Confused between 0 and '\0'

Quote:

> Simon is right in C, because in C an ordinary single-character
> character constant has type `int'.  Type `int' may have
> padding bits in C, thus '\0' may not have all bits zero in C.

> Can we stop the arguing now?

No, we can't stop arguing now, because Simon and you are no
longer right about padding bits in integer types. First you
should take a look at:

 http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_263.htm

Then, we can stop this silly discussion.

willem



Thu, 30 Sep 2004 17:34:54 GMT  
 Confused between 0 and '\0'


Quote:


>> > '\0' has a 'native' value of zero (all bits zero).

>> All value bits are zero. If the int type has any padding
>> bits, then the padding bits need not be zero.

>AFAIK, the C-committee decided padding bits are no longer
>allowed in integer types!

I'd say that is very unlikely. IIRC there may have been a decision that
all-bits-zero is guaranteed to represent a zero value for any integer
type (so memset() and calloc() can be used to produce zero valued
integers) but that doesn't rule out padding bits nor does it rule out
representations of zero that are not all-bits-zero or that 0 or '\0'
might result in such a representation.

--
-----------------------------------------


-----------------------------------------



Thu, 30 Sep 2004 18:42:03 GMT  
 Confused between 0 and '\0'

Quote:


> > Simon is right in C, because in C an ordinary single-character
> > character constant has type `int'.  Type `int' may have
> > padding bits in C, thus '\0' may not have all bits zero in C.

> > Can we stop the arguing now?

> No, we can't stop arguing now, because Simon and you are no
> longer right about padding bits in integer types. First you
> should take a look at:

>  http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_263.htm

That is only a defect report. It doesn't mean that the changes have been
accepted into the standard. And, even if it had, it does NOT make '\0'
necessarily be all-bits-zero in C.

The suggested corrigendum is "For any integer type, the object
representation where all the bits are zero shall be a representation of the
value zero in that type."

That doesn't mean that it is the only representation of the value zero. The
implementation could choose to make '\0' always correspond to one of the
other representations of the value zero, in which case it would not have
all bits zero.

Quote:
> Then, we can stop this silly discussion.

Actually Mike and I had stopped as we agree that in C++ '\0' is type char
which may not have padding bits, and in C '\0' is type int which may have
padding bits.

--
Simon.



Thu, 30 Sep 2004 21:39:29 GMT  
 Confused between 0 and '\0'


Quote:


> > Well I have searched the docs, but these are the things that I am best
> > able to find
> > 1)         '\0' has a representation of all zeros
> > 2)          Strings are terminated by '\0'

> > But is 0 gurantteed to have a have an all bit zero representation.
> > Further does that make this

> > char string[10];

> > string[0] = 0;

> > Also if we say that all bits zero is for null char then if we have

> > unsigned char c[10] ;

> > c = '\0'

> > Would it still mean all bits would be guranteed to be null,
> > specifically is it null character or null unsigned char.

> I believe it's more a matter of style to use '\0' (zero) when dealing with
> char arrays or strings and 0 (zero) for things not necessarily strings. It's
> zero either way.

<C++ specific>

It's more than a style issue. In C++ it makes a difference for
overloaded functions. For example:

void foo(int x);
void foo(char x);

foo(0) calls the first function, while foo('\0') calls the second one.

This is the main reason why character literals have type char in C++
rather than int like in C.

</C++ specific>

Bart.



Thu, 30 Sep 2004 22:33:12 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. pointer to struct I'm confused

2. If C's too confusing, read this

3. I'm confused with typedef struct...

4. I'm confused about this

5. Confused with Recordset with Data Source's records

6. I'm confused by new variable concept

7. Borland's Turbo Debugger is confused

8. I'm confused about strncat

9. '0', '1', '2', '3' ...???

10. MS-DOS NRO (an 'NROFF'- 'RNO'-like Source in C)

11. here's one that's been confusing me for a while...

12. Clarification on method modifiers 'new', 'virtual', 'override'

 

 
Powered by phpBB® Forum Software