Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: trigraphs, yecch

Reply
Thread Tools

Re: trigraphs, yecch

 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-31-2012
(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      01-31-2012
On Jan 31, 10:55*pm, Peter Nilsson <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-31-2012
Harald van Dijk <(E-Mail Removed)> 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_
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      01-31-2012
On Jan 31, 11:06*pm, (E-Mail Removed) (Ben Pfaff) wrote:
> Harald van Dijk <(E-Mail Removed)> 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.
 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      02-01-2012
"Ben Pfaff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Harald van Dijk <(E-Mail Removed)> 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 http://www.velocityreviews.com/forums/(E-Mail Removed) |
+<><><><><><><><><><><><><><><><><><><>+

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-01-2012
"Charles Richmond" <(E-Mail Removed)> writes:
> "Ben Pfaff" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Harald van Dijk <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
James Harris
Guest
Posts: n/a
 
      02-01-2012
On Feb 1, 6:20*am, Keith Thompson <(E-Mail Removed)> wrote:
> "Charles Richmond" <(E-Mail Removed)> writes:
> > "Ben Pfaff" <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-01-2012
James Harris <(E-Mail Removed)> writes:

> On Feb 1, 6:20*am, Keith Thompson <(E-Mail Removed)> wrote:
>> "Charles Richmond" <(E-Mail Removed)> writes:
>> > "Ben Pfaff" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: trigraphs, yecch Keith Thompson C Programming 20 02-07-2012 10:40 AM
Re: trigraphs, yecch Ben Bacarisse C Programming 9 02-01-2012 01:27 PM



Advertisments