Escape sequences and printing

Discussion in 'C Programming' started by Kit, Sep 16, 2003.

  1. Kit

    Kit Guest

    Hey there, and thanks for reading a newbie post. I have a lex
    assignment in which we are supposed to grab strings from a C source
    file, and print them out to the screen, with escaped characters
    "converted". Here's part of the assignment text:

    ----------------------------------------------------------------
    Example:

    main() {
    char *s = "This is string one";
    char *t = "This string has a quote (\"), an escape (\\),\nand a
    newline.";
    char this_is_not_a_string_constant = '"';
    /* "This is not a string constant, either!" */
    }

    would produce the output

    Line 2: This is string one
    Line 3: This string has a quote ("), an escape (\),
    and a newline.

    Note that escaped characters are converted, and string constants in
    comments are ignored.

    -----------------------------------------------------------------

    Now, I have it all working except the conversion of escaped
    characters. I've been trying to get a substring search and replace
    function working, to no avail. But then it occurred to me - if I'm
    printing a character array with printf, shouldn't the escaped
    characters be converted automatically? I mean, isn't that what
    they're there for? Shouldn't \\ inside a string come out as a single
    \ onscreen? And \" as a quote, and so on?

    But that's not what I'm getting. I have the following line in my
    code:

    printf( "Line %d: %s\n", (numLines + 1), yytext );

    When given the following for yytext:

    /*\\\"**/\\\tr\"\\

    It simply prints it back out exactly as above. I think I should get
    something like:

    /*\"**/\ r"\

    What am I overlooking here?

    Thanks in advance for your help,
    Chris

    PS - I should point out that I am not looking for a complete solution;
    I want to do the work myself. But it would be nice to have a nudge in
    the right direction.
     
    Kit, Sep 16, 2003
    #1
    1. Advertising

  2. Kit

    Matt Gregory Guest

    Kit wrote:

    > Hey there, and thanks for reading a newbie post. I have a lex
    > assignment in which we are supposed to grab strings from a C source
    > file, and print them out to the screen, with escaped characters
    > "converted". Here's part of the assignment text:
    >
    > ----------------------------------------------------------------
    > Example:
    >
    > main() {
    > char *s = "This is string one";
    > char *t = "This string has a quote (\"), an escape (\\),\nand a
    > newline.";
    > char this_is_not_a_string_constant = '"';
    > /* "This is not a string constant, either!" */
    > }
    >
    > would produce the output
    >
    > Line 2: This is string one
    > Line 3: This string has a quote ("), an escape (\),
    > and a newline.
    >
    > Note that escaped characters are converted, and string constants in
    > comments are ignored.
    >
    > -----------------------------------------------------------------
    >
    > Now, I have it all working except the conversion of escaped
    > characters. I've been trying to get a substring search and replace
    > function working, to no avail. But then it occurred to me - if I'm
    > printing a character array with printf, shouldn't the escaped
    > characters be converted automatically? I mean, isn't that what
    > they're there for? Shouldn't \\ inside a string come out as a single
    > \ onscreen? And \" as a quote, and so on?
    >
    > But that's not what I'm getting. I have the following line in my
    > code:
    >
    > printf( "Line %d: %s\n", (numLines + 1), yytext );
    >
    > When given the following for yytext:
    >
    > /*\\\"**/\\\tr\"\\
    >
    > It simply prints it back out exactly as above. I think I should get
    > something like:
    >
    > /*\"**/\ r"\
    >
    > What am I overlooking here?
    >
    > Thanks in advance for your help,
    > Chris
    >
    > PS - I should point out that I am not looking for a complete solution;
    > I want to do the work myself. But it would be nice to have a nudge in
    > the right direction.


    You can do it much more easily with start states.

    Matt Gregory
     
    Matt Gregory, Sep 16, 2003
    #2
    1. Advertising

  3. Kit

    Colin Newell Guest

    The escape characters are a feature of the language, not the printf call.
    The escape characters get expanded at compile time, not runtime so they will
    not be of any use to you.
     
    Colin Newell, Sep 16, 2003
    #3
  4. Kit

    Matt Gregory Guest

    Colin Newell wrote:

    > The escape characters are a feature of the language, not the printf call.
    > The escape characters get expanded at compile time, not runtime so they will
    > not be of any use to you.


    But compile time is what he's talking about. He's using lex...
     
    Matt Gregory, Sep 16, 2003
    #4
  5. Kit

    Colin Newell Guest

    He's trying to use printf to expand out a variable, it's definately an
    attempt at a runtime hack.

    Colin.
     
    Colin Newell, Sep 16, 2003
    #5
  6. Kit

    Matt Gregory Guest

    Colin Newell wrote:

    > He's trying to use printf to expand out a variable, it's definately an
    > attempt at a runtime hack.


    Oh, you're right, sorry. I must learn to pay attention.
     
    Matt Gregory, Sep 17, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,446
    Mike Treseler
    Nov 28, 2005
  2. Anton81
    Replies:
    6
    Views:
    405
    Dennis Lee Bieber
    Aug 10, 2006
  3. slomo
    Replies:
    5
    Views:
    1,546
    Duncan Booth
    Dec 2, 2007
  4. Guest
    Replies:
    2
    Views:
    566
    Tim Roberts
    Dec 15, 2007
  5. Guest
    Replies:
    4
    Views:
    715
    Martin v. Löwis
    Dec 19, 2007
Loading...

Share This Page