Re: trigraphs, yecch

Discussion in 'C Programming' started by Ben Bacarisse, Jan 31, 2012.

  1. pete <> writes:

    > Richard Harter wrote:
    >>
    >> The other day I wanted to put three successive question marks in a
    >> string. Frex, "(???).%s.%s". My trusty antique gcc compiler
    >> converted the last two '?'s into a ']' along with a warning that it
    >> was doing a trigraph conversion.
    >>
    >> Now, me, I know from nothing about trigraphs - never used them, hope
    >> never to use them - so I was caught by surprise. My elderly copy of
    >> K&R described them but I didn't see anything about getting around
    >> them.
    >>
    >> So. How is one supposed to get three successive question marks into a
    >> string?

    >
    > "(?""?""?).%s.%s\n"


    That's possibly a bit "belt and braces". "(??""?).%s.%s" will do.

    --
    Ben.
    Ben Bacarisse, Jan 31, 2012
    #1
    1. Advertising

  2. Ben Bacarisse

    James Kuyper Guest

    On 01/31/2012 12:21 PM, Richard Harter wrote:
    ....
    > ... Are you
    > telling me that two adjacent strings will be merged into one string.


    Yes. This merger is the only thing that occurs during translation phase
    6, which is described in section 5.1.1.2p6.
    James Kuyper, Jan 31, 2012
    #2
    1. Advertising

  3. Ben Bacarisse

    ImpalerCore Guest

    On Jan 31, 12:21 pm, (Richard Harter) wrote:
    > On Tue, 31 Jan 2012 16:47:45 +0000, Ben Bacarisse
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > <> wrote:
    > >pete <> writes:

    >
    > >> Richard Harter wrote:

    >
    > >>> The other day I wanted to put three successive question marks in a
    > >>> string.  Frex, "(???).%s.%s".  My trusty antique gcc compiler
    > >>> converted the last two '?'s into a ']' along with a warning that it
    > >>> was doing a trigraph conversion.

    >
    > >>> Now, me, I know from nothing about trigraphs - never used them, hope
    > >>> never to use them - so I was caught by surprise.  My elderly copy of
    > >>> K&R described them but I didn't see anything about getting around
    > >>> them.

    >
    > >>> So.  How is one supposed to get three successive question marks into a
    > >>> string?

    >
    > >> "(?""?""?).%s.%s\n"

    >
    > >That's possibly a bit "belt and braces".  "(??""?).%s.%s" will do.

    >
    > That doesn't seem right to me, but what do I know.  As I read that
    > there are two strings there, to wit "(??" and "?).%s.%s".  Are you
    > telling me that two adjacent strings will be merged into one string.
    > If that's so I'm certainly surprised and I've learned something quite
    > unexpected.  Does this mean I can write  printf("x""y") and I will get
    > "xy" as an output?


    Yep. That behavior is actually required to use PRI fprintf macros.

    printf( "INT32_MIN = %" PRId32 ", INT32_MAX = %" PRId32 "\n",
    INT32_MIN, INT32_MAX );

    where PRId32 may be defined to "d" or "ld" or "I32d" or whatever
    string is needed.

    Best regards,
    John D.
    ImpalerCore, Jan 31, 2012
    #3
  4. (Richard Harter) writes:

    > On Tue, 31 Jan 2012 16:47:45 +0000, Ben Bacarisse
    > <> wrote:
    >
    >>pete <> writes:
    >>
    >>> Richard Harter wrote:
    >>>>
    >>>> The other day I wanted to put three successive question marks in a
    >>>> string. Frex, "(???).%s.%s". My trusty antique gcc compiler
    >>>> converted the last two '?'s into a ']' along with a warning that it
    >>>> was doing a trigraph conversion.
    >>>>
    >>>> Now, me, I know from nothing about trigraphs - never used them, hope
    >>>> never to use them - so I was caught by surprise. My elderly copy of
    >>>> K&R described them but I didn't see anything about getting around
    >>>> them.
    >>>>
    >>>> So. How is one supposed to get three successive question marks into a
    >>>> string?
    >>>
    >>> "(?""?""?).%s.%s\n"

    >>
    >>That's possibly a bit "belt and braces". "(??""?).%s.%s" will do.

    >
    > That doesn't seem right to me, but what do I know. As I read that
    > there are two strings there, to wit "(??" and "?).%s.%s". Are you
    > telling me that two adjacent strings will be merged into one string.


    Yes.

    > If that's so I'm certainly surprised and I've learned something quite
    > unexpected. Does this mean I can write printf("x""y") and I will get
    > "xy" as an output?


    Yup. It's not an uncommon idiom. For example, it's used by the print
    format macros in inttypes.h. PRId64 is a string with the correct format
    for an int64_t: printf("%10"PRId64"\n", x); You can also use it to
    interpolate a macro into a string to make scanf("%" TO_STR(SIZE) "s"...)
    safe. (But note that the target must be of size SIZE+1.)

    C translation occurs in a number of phases. Trigraphs are translated in
    phase 1 and adjacent string literals are fused in phase 6 (after macro
    expansion in phase 4) so simply splitting a trigraph is enough.

    --
    Ben.
    Ben Bacarisse, Jan 31, 2012
    #4
  5. Robert Wessel <> writes:
    [...]
    > Yes. C merges adjacent strings.

    [...]

    C merges adjacent string *literals*.

    For example, this:

    printf("hello" " world\n");

    works, but this:

    const char s[] = "hello";
    printf(s " world\n");

    is a syntax error.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 31, 2012
    #5
  6. Ben Bacarisse

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Robert Wessel <> writes:
    > [...]
    >> Yes. C merges adjacent strings.

    > [...]
    >
    > C merges adjacent string *literals*.
    >
    > For example, this:
    >
    > printf("hello" " world\n");
    >
    > works, but this:
    >
    > const char s[] = "hello";
    > printf(s " world\n");
    >
    > is a syntax error.


    On the other hand, this works as expected:

    #define s "hello"

    printf(s " world\n");

    Is s a string literal here?

    --
    Bartc
    BartC, Feb 1, 2012
    #6
  7. Ben Bacarisse

    James Kuyper Guest

    On 01/31/2012 08:09 PM, BartC wrote:
    >
    >
    > "Keith Thompson" <> wrote in message
    > news:...

    ....
    >> C merges adjacent string *literals*.
    >>
    >> For example, this:
    >>
    >> printf("hello" " world\n");
    >>
    >> works, but this:
    >>
    >> const char s[] = "hello";
    >> printf(s " world\n");
    >>
    >> is a syntax error.

    >
    > On the other hand, this works as expected:
    >
    > #define s "hello"
    >
    > printf(s " world\n");
    >
    > Is s a string literal here?


    No, s is the name of a macro; but during translation phase 4 that macro
    expands into a string literal.
    --
    James Kuyper
    James Kuyper, Feb 1, 2012
    #7
  8. "BartC" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Robert Wessel <> writes:
    >> [...]
    >>> Yes. C merges adjacent strings.

    >> [...]
    >>
    >> C merges adjacent string *literals*.
    >>
    >> For example, this:
    >>
    >> printf("hello" " world\n");
    >>
    >> works, but this:
    >>
    >> const char s[] = "hello";
    >> printf(s " world\n");
    >>
    >> is a syntax error.

    >
    > On the other hand, this works as expected:
    >
    > #define s "hello"
    >
    > printf(s " world\n");
    >
    > Is s a string literal here?


    It is by the time adjacent string literals are merged.

    Macro invocations are expanded in translation phase 4. Adjacent string
    literals are merged in translation phase 6.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 1, 2012
    #8
  9. "Charles Richmond" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Robert Wessel <> writes:
    >> [...]
    >>> Yes. C merges adjacent strings.

    >> [...]
    >>
    >> C merges adjacent string *literals*.
    >>
    >> For example, this:
    >>
    >> printf("hello" " world\n");
    >>
    >> works, but this:
    >>
    >> const char s[] = "hello";
    >> printf(s " world\n");
    >>
    >> is a syntax error.
    >>

    >
    > But you *can* do something like this, using the preprocessor:
    >
    >
    > #define HELLO "hello"
    > #define WORLD "world!!!\n"
    >
    > ... .... ..... .....
    >
    > printf(HELLO WORLD);


    Sure, but that doesn't work if the strings are not compile-time constant
    values. There's no way to use string concatenation to do something like
    this:

    char line[80];
    fgets(line, sizeof line, stdin);
    puts("line = " line);

    > Keith, your sig might say "DO right code for food." After all, if
    > programming in C is your occupation, that is basically what you are doing.
    > :)


    <OT>It's a reference to the "Will work for food" signs that some
    homeless/unemployed people carry. I'm currently unemployed.</OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 1, 2012
    #9
  10. Ben Bacarisse

    James Kuyper Guest

    On 02/01/2012 01:19 AM, Keith Thompson wrote:
    ....
    > <OT>It's a reference to the "Will work for food" signs that some
    > homeless/unemployed people carry. I'm currently unemployed.</OT>


    If you're serious about "Will write code for food", you should provide a
    link that leads to your resume.
    --
    James Kuyper
    James Kuyper, Feb 1, 2012
    #10
    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. Christopher Benson-Manica

    Trigraphs

    Christopher Benson-Manica, Sep 12, 2003, in forum: C Programming
    Replies:
    33
    Views:
    1,308
    Tom Zych
    Sep 18, 2003
  2. Vijay Kumar R Zanvar

    Trigraphs & entry

    Vijay Kumar R Zanvar, Nov 4, 2003, in forum: C Programming
    Replies:
    14
    Views:
    655
    Mark McIntyre
    Nov 5, 2003
  3. Daniel Rudy

    Trigraphs

    Daniel Rudy, Nov 7, 2005, in forum: C Programming
    Replies:
    39
    Views:
    1,296
    Michael Wojcik
    Jan 6, 2006
  4. Peter Nilsson

    Re: trigraphs, yecch

    Peter Nilsson, Jan 31, 2012, in forum: C Programming
    Replies:
    7
    Views:
    206
    Ben Bacarisse
    Feb 1, 2012
  5. Keith Thompson

    Re: trigraphs, yecch

    Keith Thompson, Jan 31, 2012, in forum: C Programming
    Replies:
    20
    Views:
    499
    Joachim Schmitz
    Feb 7, 2012
Loading...

Share This Page