Re: trigraphs, yecch

Discussion in 'C Programming' started by Peter Nilsson, Jan 31, 2012.

  1. (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.
    > ...
    > So.  How is one supposed to get three successive question marks
    > into a string?


    Simple. You put three successive question marks in a string. ;)
    E.g. "(" "???" ").%s.%s".

    As you know, the issue is not a sequence of three question marks,
    but the ) that follows the last two. Old MacOS programmers
    encountered the trigraph issue with the character constant '????'.
    The adopted idiom was to use '???\?'. Whilst it looks wierd at
    first sight, Mac programmers wouldn't bat an eyelid at it.

    --
    Peter
     
    Peter Nilsson, Jan 31, 2012
    #1
    1. Advertising

  2. On Jan 31, 10:55 pm, Peter Nilsson <> wrote:
    > Old MacOS programmers
    > encountered the trigraph issue with the character constant '????'.
    > The adopted idiom was to use '???\?'. Whilst it looks wierd at
    > first sight, Mac programmers wouldn't bat an eyelid at it.


    Out of curiosity: what is the correct way to avoid this?
    Multicharacter constants are implementation-specific but valid in
    standard C, and at the same time \? is not. I can only come up with

    '???\
    ?'

    but that seems more complicated than should be necessary.
     
    Harald van Dijk, Jan 31, 2012
    #2
    1. Advertising

  3. Peter Nilsson

    Ben Pfaff Guest

    Harald van Dijk <> writes:

    > Multicharacter constants are implementation-specific but valid in
    > standard C, and at the same time \? is not.


    \? is a valid escape sequence in a character or string constant
    in standard C, since C89.
    --
    "When in doubt, treat ``feature'' as a pejorative.
    (Think of a hundred-bladed Swiss army knife.)"
    --Kernighan and Plauger, _Software Tools_
     
    Ben Pfaff, Jan 31, 2012
    #3
  4. On Jan 31, 11:06 pm, (Ben Pfaff) wrote:
    > Harald van Dijk <> writes:
    > > Multicharacter constants are implementation-specific but valid in
    > > standard C, and at the same time \? is not.

    >
    > \? is a valid escape sequence in a character or string constant
    > in standard C, since C89.


    Oops, you're quire right. I'm not sure why I thought it was invalid.
     
    Harald van Dijk, Jan 31, 2012
    #4
  5. "Ben Pfaff" <> wrote in message
    news:...
    > Harald van Dijk <> writes:
    >
    >> Multicharacter constants are implementation-specific but valid in
    >> standard C, and at the same time \? is not.

    >
    > \? is a valid escape sequence in a character or string constant
    > in standard C, since C89.
    >

    Yup... when in doubt, just escape them all:

    printf(" These are \?\?\?\?\?.\n");


    --
    +<><><><><><><><><><><><><><><><><><><>+
    | Charles Richmond |
    +<><><><><><><><><><><><><><><><><><><>+
     
    Charles Richmond, Feb 1, 2012
    #5
  6. "Charles Richmond" <> writes:
    > "Ben Pfaff" <> wrote in message
    > news:...
    >> Harald van Dijk <> writes:
    >>
    >>> Multicharacter constants are implementation-specific but valid in
    >>> standard C, and at the same time \? is not.

    >>
    >> \? is a valid escape sequence in a character or string constant
    >> in standard C, since C89.
    >>

    > Yup... when in doubt, just escape them all:
    >
    > printf(" These are \?\?\?\?\?.\n");


    When in doubt, learn the rules, thereby removing the doubt.

    --
    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
    #6
  7. Peter Nilsson

    James Harris Guest

    On Feb 1, 6:20 am, Keith Thompson <> wrote:
    > "Charles Richmond" <> writes:
    > > "Ben Pfaff" <> wrote in message


    ....

    > >> \? is a valid escape sequence in a character or string constant
    > >> in standard C, since C89.

    >
    > > Yup... when in doubt, just escape them all:

    >
    > >     printf(" These are \?\?\?\?\?.\n");

    >
    > When in doubt, learn the rules, thereby removing the doubt.


    Easy to say. C has lots of rules, though. Charles' simplifying
    suggestion looks good to me as it leaves a reader in no doubt of the
    intention.

    James
     
    James Harris, Feb 1, 2012
    #7
  8. James Harris <> writes:

    > On Feb 1, 6:20 am, Keith Thompson <> wrote:
    >> "Charles Richmond" <> writes:
    >> > "Ben Pfaff" <> wrote in message

    >
    > ...
    >
    >> >> \? is a valid escape sequence in a character or string constant
    >> >> in standard C, since C89.

    >>
    >> > Yup... when in doubt, just escape them all:

    >>
    >> >     printf(" These are \?\?\?\?\?.\n");

    >>
    >> When in doubt, learn the rules, thereby removing the doubt.

    >
    > Easy to say. C has lots of rules, though. Charles' simplifying
    > suggestion looks good to me as it leaves a reader in no doubt of the
    > intention.


    Only if you know the rules about \ in strings so you know that there
    are no special rules that apply to sequences like \?\, say. Maybe
    string characters are escaped before trigraph processing occurs, or
    maybe... In the end, the meaning of a line of C can't really be in no
    doubt unless the reader knows almost all of the rules.

    But compilers know the rules and they can often help. For example, by
    including -Werror=trigraphs in your gcc options, compilation will fail
    if any trigraph gets translated.

    --
    Ben.
     
    Ben Bacarisse, Feb 1, 2012
    #8
    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,317
    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:
    663
    Mark McIntyre
    Nov 5, 2003
  3. Daniel Rudy

    Trigraphs

    Daniel Rudy, Nov 7, 2005, in forum: C Programming
    Replies:
    39
    Views:
    1,315
    Michael Wojcik
    Jan 6, 2006
  4. Ben Bacarisse

    Re: trigraphs, yecch

    Ben Bacarisse, Jan 31, 2012, in forum: C Programming
    Replies:
    9
    Views:
    266
    James Kuyper
    Feb 1, 2012
  5. Keith Thompson

    Re: trigraphs, yecch

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

Share This Page