Kenneth Brody said:
Philip said:
somenath wrote: [...]
So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 . Is my understanding is correct ?
No, '1' (the character) is wholly unrelated to 1 (the integer constant).
The fact that '1'+1 == '2' makes sense mathematically is pure
coincidence. Other mathematical constructs are not likely to work on
numerical characters, for example '1'*2 == '2' is guaranteed to be
false.
I think you mean "is not guaranteed to be true".
I thought so as well when I first read that, but no, I think he meant
"is guaranteed to be false", for reasons you address below.
... in any character set with which you are peronally familiar.
I don't believe that anything in the Standard would forbid such a
character set, as long as '0' through '9' were contiguous.
On second thought, if '1'==1 were true, then '0'==0 and '0'=='\0',
which would be a bad thing. Does the Standard address this?
Certainly an implementation with '0' == '\0' would have serious
problems; for one thing, a string could not contain the '0' character
(except as the terminator). Whether the standard specifically forbids
such an implementation is another question.
C99 5.2.1p2 says:
A byte with all bits set to 0, called the _null character_, shall
exist in the basic execution character set; it is used to
terminate a character string.
That section also lists the other members of the basic execution
character set, including the digits '0' .. '9'. One could infer that
all members of the basic execution character set must be distinct, so
'0' != '\0', but it's not *quite* explicitly stated.
C99 6.2.5p3 says:
If a member of the basic execution character set is stored in a
char object, its value is guaranteed to be nonnegative.
which doesn't *by itself* forbid '0' == '\0'.
I believe it's the intent to forbid '0' == '\0', but I haven't (so
far) been able to find explicit wording to that effect in the
standard, either because I haven't searched thoroughly enough or
because the authors thought it was too obvious to bother stating.