Escape sequence and character set problem

Discussion in 'C++' started by nvangogh, Mar 13, 2014.

  1. nvangogh

    nvangogh Guest

    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?
     
    nvangogh, Mar 13, 2014
    #1
    1. Advertisements

  2. 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
     
    Victor Bazarov, Mar 13, 2014
    #2
    1. Advertisements

  3. nvangogh

    nvangogh Guest

    #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.
     
    nvangogh, Mar 13, 2014
    #3
  4. nvangogh

    nvangogh Guest

    Sorry \012 represents a single newline
     
    nvangogh, Mar 13, 2014
    #4
  5. 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)
     
    Miguel Giménez, Mar 13, 2014
    #5
  6. Thank you for correcting me Miguel! Shows how much I use escape
    sequences in my everyday programming life... :-/

    V
     
    Victor Bazarov, Mar 13, 2014
    #6
  7. nvangogh

    nvangogh Guest

    Can you tell me what encoding chart I need to look at to see these values?
     
    nvangogh, Mar 13, 2014
    #7
  8. El 13/03/2014 19:13, nvangogh escribió:

    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
     
    Miguel Giménez, Mar 13, 2014
    #8
  9. Use an ACSII table. On linux, 'man ascii'. Otherwise, use a site
    like this:
    http://www.asciitable.com/
     
    Randy Westlund, Mar 13, 2014
    #9
  10. nvangogh

    nvangogh Guest

    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;
    }
     
    nvangogh, Mar 13, 2014
    #10
  11. Haha, nice. Glad I could help :)
     
    Randy Westlund, Mar 13, 2014
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.