Escape sequence and character set problem

N

nvangogh

What values does this literal represent? What type does it have?
"Who goes with F\145rgus?\012"

This is from C++ Primer exercise 2.7 (p41 of 5th ed)

I cannot figure this out.

F is a suffix for float that only makes sense to me if numeric value
came before it. But it comes before an escape sequence. What does \145
mean? Taken as a whole can you explain how this line is put together and
how I can interpret it?
 
V

Victor Bazarov

What values does this literal represent? What type does it have?
"Who goes with F\145rgus?\012"

This is from C++ Primer exercise 2.7 (p41 of 5th ed)

I cannot figure this out.

F is a suffix for float that only makes sense to me if numeric value
came before it. But it comes before an escape sequence. What does \145
mean? Taken as a whole can you explain how this line is put together and
how I can interpret it?

Inside a string literal, letter F has no special meaning. It's just a
letter. However, \145 is an escape sequence designating a special
character (most likely) with the value of decimal 145, which is actually
not possible in a basic character set limited to -127..127. So, most
likely it's substituted with char(255-145), some negative value, but
will yield the same thing however. Look up in the extended ASCII table
to see what your computer is likely to output. Or just write a small
program to output that literal. If we designate that character with the
asterisk (like in Scrabble) and the newline (the second escape sequence
is the line feed character) with the tilda, the literal becomes "Who
goes with F*rgus?~"

What it will do when you output it depends on the device, though.

V
 
N

nvangogh

Inside a string literal, letter F has no special meaning. It's just a
letter. However, \145 is an escape sequence designating a special
character (most likely) with the value of decimal 145, which is actually
not possible in a basic character set limited to -127..127. So, most
likely it's substituted with char(255-145), some negative value, but
will yield the same thing however. Look up in the extended ASCII table
to see what your computer is likely to output. Or just write a small
program to output that literal. If we designate that character with the
asterisk (like in Scrabble) and the newline (the second escape sequence
is the line feed character) with the tilda, the literal becomes "Who
goes with F*rgus?~"

What it will do when you output it depends on the device, though.

V
#include <iostream>

int main()
{
std::cout << "Who goes with F\145rgus?\012" << std::endl;
return 0;
}

Outputs "Who goes with Fergus?" followed by what looks like a double new
line.
 
N

nvangogh

#include <iostream>

int main()
{
std::cout << "Who goes with F\145rgus?\012" << std::endl;
return 0;
}

Outputs "Who goes with Fergus?" followed by what looks like a double new
line.
Sorry \012 represents a single newline
 
M

Miguel Giménez

El 13/03/2014 18:20, nvangogh escribió:
Sorry \012 represents a single newline

The \nnn is a escape sequence in octal, so \145 translates to decimal
101 (ASCII 101 = 'e') and \012 to decimal 10 (ASCII 10 = LINE FEED)
 
V

Victor Bazarov

[..]
The \nnn is a escape sequence in octal, so \145 translates to decimal
101 (ASCII 101 = 'e') and \012 to decimal 10 (ASCII 10 = LINE FEED)

Thank you for correcting me Miguel! Shows how much I use escape
sequences in my everyday programming life... :-/

V
 
N

nvangogh

El 13/03/2014 18:20, nvangogh escribió:

The \nnn is a escape sequence in octal, so \145 translates to decimal
101 (ASCII 101 = 'e') and \012 to decimal 10 (ASCII 10 = LINE FEED)
Can you tell me what encoding chart I need to look at to see these values?
 
M

Miguel Giménez

El 13/03/2014 19:13, nvangogh escribió:

Can you tell me what encoding chart I need to look at to see these values?

There is no encoding chart, it's just a base change from Base-8 to
Base-10. 145 octal means (in decimal) 1*64+4*8+5 = 101, 012 means
0*64+1*8+2 = 10.

You can use your OS calculator in scientific or programmer mode to make
the conversion, just like you change from radians to gradians.

http://en.wikipedia.org/wiki/Octal
 
N

nvangogh

Use an ACSII table. On linux, 'man ascii'. Otherwise, use a site
like this:
http://www.asciitable.com/
Hey - run this when you get the chance:

#include <iostream>
int main()
{
std::cout << "\124\110\101\116\113\123\041" << std::endl;
std::cout << "\165\163\145\146\165\154\040\114\151\156\153" << std::endl;
return 0;
}
 
R

Randy Westlund

Hey - run this when you get the chance:

#include <iostream>
int main()
{
std::cout << "\124\110\101\116\113\123\041" << std::endl;
std::cout << "\165\163\145\146\165\154\040\114\151\156\153" << std::endl;
return 0;
}

Haha, nice. Glad I could help :)
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top