octal character constants
Jeff Smith
jeff at warwick.UUCP
Thu Apr 10 22:59:25 AEST 1986
There is a comment in the C++ Release 1 translator
sizeof("\01") a syntax error (c_strlen in size.c)
The relevant part of K+R says:
`The escape \ddd consists of the backslash followed by 1, 2, or
3 octal digits which are taken to specify the value of the desired character.
A special case of this construction is \0 (not followed by a digit), which
indicates the character NUL.' (p181)
Bjarne's manual says the same. (p246)
I claim that the string "p\01q" is legal, has four characters in it (p, SOH,
q and NUL) and will be passed through as is by cfront (whereas \xddd (c++ hex
feature) constructs will always generate escape sequences with the full three
digits). In which case c_strlen should be replaced by the following code:
#ifdef OCTALDDDFIX
#include <ctype.h>
static inline
isoctal(char c) {
return (isascii(c) && isdigit(c) && c != '9' && c != '8');
}
int c_strlen(char* s) {
int i = 1;
for (register char* p = s; *p; i++,p++)
if (*p == '\\') {
if (isoctal(*++p)) {
if (! isoctal(*++p))
p--;
else if (! isoctal(*++p))
p--;
}
}
return i;
}
#else !OCTALDDDFIX
int c_strlen(char* s)
/*
return sizeof(s) with escapes processed
sizeof("") == 1 the terminating 0
sizeof("a") == 2
sizeof("\0x") == 3 0 x 0
sizeof("\012") == 2 '\012'
sizeof("\01") a syntax error
sizeof("\x") == 2 \ ignored
*/
{
/*
old body, not quoted here lest the ghost of Old Ma Bell
haunt me forever
*/
}
#endif OCTALDDDFIX
Or maybe Bjarne intended the chconst code in lex.c to translate all
character constants to three-digit form. Bjarne?
--------------------------------------
...mcvax!warwick!jeff (uucp)
..jeff%warwick.uucp at ucl-cs.arpa (arpa)
More information about the Comp.lang.c
mailing list