The Mask
The Mask

Reputation: 17447

How is handled escape values(like \nnn,\xnn) in string literal?

How is handled escape values(like \nnn,\xnn) in string literal? consider

"foo \x61 \042 baa"

Is \x61 and \042 converted to decimal base and stored in a memory location? and is translated to "foo 97 34 baa" if so,is this at compile-time? does it applies to all escape characters? if don't,how is this handle by the C compiler?

Upvotes: 0

Views: 594

Answers (1)

Kevin
Kevin

Reputation: 56129

All characters in (or not in, for that matter) a string are just numbers. In c a string is stored as an array of 8-bit numbers with a 0 at the end to indicate the end of the string (the "null terminator"). Escape sequences are handled in precisely the same manner as regular characters: their ascii value is stored in an array. In your example, the following are identical:

"foo \x61 \042 baa"
"foo a \" baa"

{ 'f',  'o',  'o',  ' ', '\x61',  ' ', '\042',  ' ',  'b',  'a',  'a', '\0'}
{ 'f',  'o',  'o',  ' ',    'a',  ' ',    '"',  ' ',  'b',  'a',  'a', '\0'}
{ 'f',  'o',  'o',  ' ',   0x61,  ' ',    042,  ' ',  'b',  'a',  'a', '\0'}
{0x66, 0x6f, 0x6f, 0x20,   0x61, 0x20,   0x22, 0x20, 0x62, 0x61, 0x61,  0x0}
{ 102,  111,  111,   32,     97,   32,     34,   32,   98,   97,   97,    0}
{0146, 0157, 0157,  040,   0141,  040,    042,  040, 0142, 0141, 0141,    0}

Upvotes: 3

Related Questions