0 vs '\0'

A

Anand

Irrwahn said:
Keith Thompson said:
<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
ITYM '\x' not '/x'
 
A

Anand

Keith said:
Yes (and no, it's not really off-topic). See below
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".
I knew this(i.e. 0ddd, 0x, 0X etc.) but i was just referring to just 0.
I should've reworded it.
0 is an octal constant.
This answers my question.

So the compiler has no knowledge of decimal-0.. it knows either octal 0
(0) or a hexa 0 (0x0, 0X0)

The reason for OT was : I thought how compiler (token-parser) internally
thinks of 0 is implementation specific. An Octal 0 or a hexa 0 or a
decimal 0 , will all have the same representation ultimatly and this
doesn't matter (or does it?) ...

[pardon my gibberish-brain]
 
A

Anand

Keith said:
Yes (and no, it's not really off-topic).
See below (at the end)
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".

I knew this(i.e. 123, 0123, 0x123, 0X123 etc.) but I was just referring
to just 0. I should've reworded it.
0 is an octal constant.

This answers my question.


So the compiler has no knowledge of decimal-0.. it knows either octal 0
(0) or a hex 0 (0x0, 0X0)


The reason for OT was : I thought how compiler (token-parser) internally
thinks of 0 is implementation specific. An Octal 0 or a hex 0 or a
decimal 0 , will all have the same representation ultimately and this
doesn't matter (or does it?) ...

[pardon my gibberish brain]
 
R

Richard Bos

Anand said:
This answers my question.

So the compiler has no knowledge of decimal-0.. it knows either octal 0
(0) or a hex 0 (0x0, 0X0)

As part of the source code, yes. They all boil down to the same thing,
though, so it doesn't matter.
The reason for OT was : I thought how compiler (token-parser) internally
thinks of 0 is implementation specific. An Octal 0 or a hex 0 or a
decimal 0 , will all have the same representation ultimately and this
doesn't matter (or does it?) ...

Precisely. Just as 0x50 has the same representation as 80 and 0100.

Richard
 
K

Keith Thompson

Anand said:
Keith Thompson wrote: [...]
0 is an octal constant.

This answers my question.


So the compiler has no knowledge of decimal-0.. it knows either octal
0 (0) or a hex 0 (0x0, 0X0)


The reason for OT was : I thought how compiler (token-parser)
internally thinks of 0 is implementation specific. An Octal 0 or a hex
0 or a decimal 0 , will all have the same representation ultimately
and this doesn't matter (or does it?) ...

[pardon my gibberish brain]

As far as the language is concerned, 0 is an octal constant. A
compiler can choose to treat it internally as a decimal constant;
there should be no visible consequences. There's also no good reason
to do so; it would have to be done as a special case, and the compiler
has to handle octal constants anyway.
 
J

Joe Wright

Jack said:
But the character literal '0' appearing unadorned in C source is also
an octal constant expression.
#include <stdio.h>

int main(void) {
int i;
i = 0;
printf("%d\n",i);
i = '\0';
printf("%d\n",i);
i = '0'; /* Octal Constant? */
printf("%d\n",i);
return 0;
}
...prints..
0
0
48

What am I missing?
 
C

C Wegrzyn

You aren't missing anything - the code is doing exactly what it is
supposed to be doing.
The constants 0 and \0 are the binary number 0, where as '0' will be the
binary value of the character 0.
Did you think '0' is the value 0? If so, that isn't correct.

Chuck Wegrzyn
 
K

Keith Thompson

C Wegrzyn said:
You aren't missing anything - the code is doing exactly what it is
supposed to be doing.
The constants 0 and \0 are the binary number 0, where as '0' will be the
binary value of the character 0.
Did you think '0' is the value 0? If so, that isn't correct.

First, please don't top-post (corrected).

Second, you missed the point yourself. Jack Klein was responding
to Joe Wright's statement that

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

which is incorrect. The character constant '0' has an
implementation-defined value of type int which is guaranteed not to be
equal to 0 (it's 48 in ASCII). Yes, the code does exactly what it's
supposed to do -- namely illustrate that '0' is not an octal constant.
 
C

Christopher Benson-Manica

Keith Thompson said:
Second, you missed the point yourself. Jack Klein was responding
to Joe Wright's statement that
But the character literal '0' appearing unadorned in C source is
also an octal constant expression.

It was actually Joe Wright responding to Jack Klein's statement.
 

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