Reputation: 1156
Going through the 5th edition of the C++ Primer book I was told that integers literals that begin with 0
are interpreted as octal
. It left me with this example on how the integer literal 20
is represented in the following notations:
20 `/* decimal */` 024 `/* octal */` 0x14 `/* hexadecimal */`
Advancing a couple of pages, I was then introduced to escape characters and how to represent numerical values of characters with them using octal
and hexadecimal
digits. I was then presented with this...
std::cout << "Hi \x4dO\115!\n"; // prints Hi MOM! followed by a newline
std::cout << '\115' << '\n'; // prints M followed by a newline
Note that if a
\
is followed by more than threeoctal
digits, only the first three are associated with the\
. For example,\1234
represents two characters: the character represented by theoctal
value123
and the character4
. In contrast,\x
uses up all the hex digits following it;\x1234
represents a single, 16-bit character composed from the bits corresponding to these fourhexadecimal
digits. Because most machines have 8-bit chars, such values are unlikely to be useful. Ordinarily,hexadecimal
characters with more than 8 bits are used with extended characters sets using one of the prefixes from Table 2.2.
Ok so is it that decimal
isn't used with escape characters when representing the numerical value of chars, or the whole interpretation of what is defined as a octal
is changed with escape characters ? I'm confused because I thought octals
are interpreted as literals that begin with 0
if it isn't followed by an x
, but what I'm reading from that previous block quote...
For example,
\1234
represents two characters: the character represented by theoctal
value123
and the character4
.
It says the octal value 123
, but there is no 0
. So is it only with escape characters that they truncate the zero, and if so then how is a decimal
literal represented when using escape characters ?
Upvotes: 1
Views: 855
Reputation: 206707
Characters can be defined using the following escape sequences.
From the C++11 Standard:
2.14.3 Character literals
simple-escape-sequence: one of \’ \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence: \ octal-digit \ octal-digit octal-digit \ octal-digit octal-digit octal-digit hexadecimal-escape-sequence: \x hexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit
A string literal can use any of the escape sequences used to define character literals.
Coming to your question ...
Ok so is it that decimal isn't used with escape characters when representing the numerical value of chars,
The answer is "Yes". Decimals cannot be used to define escape sequences of character literals or characters in string literals.
I'm confused because I thought octals are interpreted as literals that begin with 0 if it isn't followed by an x,
That is true only for integer literals, not when being used as escape sequences for character literals and characters in string literals.
Upvotes: 2
Reputation: 303527
It says the octal value 123, but there is no 0. So is it only with escape characters that they truncate the zero, and if so then how is a decimal literal represented when using escape characters ?
That is correct - you do not need zero to have an octal escape sequence. The grammar is defined in [lex.ccon] as:
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\ hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
Or alternatively:
The escape
\ooo
consists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character. The escape\xhhh
consists of the backslash followed by x followed by one or more hexadecimal digits that are taken to specify the value of the desired character. There is no limit to the number of digits in a hexadecimal sequence. A sequence of octal or hexadecimal digits is terminated by the first character that is not an octal digit or a hexadecimal digit, respectively. The value of a character literal is implementation-defined if it falls outside of the implementation-defined range defined forchar
(for literals with no prefix) orwchar_t
(for literals prefixed by L).
So \19
would be an octal literal with value 1 followed by a decimal literal with value 9. There is no decimal escape sequence. The full table of possibilities is enumerated here.
Upvotes: 2
Reputation: 180955
An octal literal needs to start with a 0
. This is what tells the compiler that we are using a octal number. When using the escape character the number(s) after it are treated as an octal number and it does not need to be prefixed with a 0
as the compiler know it is an octal number. This is why \x
is needed if you want to use a hex number.
Cppreference has a good table showing all of the escape sequences.
Upvotes: 2