Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: trigraphs, yecch

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-31-2012
pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      01-31-2012
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.
 
Reply With Quote
 
 
 
 
ImpalerCore
Guest
Posts: n/a
 
      01-31-2012
On Jan 31, 12:21*pm, (E-Mail Removed) (Richard Harter) wrote:
> On Tue, 31 Jan 2012 16:47:45 +0000, Ben Bacarisse
>
>
>
>
>
>
>
>
>
> <(E-Mail Removed)> wrote:
> >pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-31-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Harter) writes:

> On Tue, 31 Jan 2012 16:47:45 +0000, Ben Bacarisse
> <(E-Mail Removed)> wrote:
>
>>pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-31-2012
Robert Wessel <(E-Mail Removed)> 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) (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
 
BartC
Guest
Posts: n/a
 
      02-01-2012


"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Robert Wessel <(E-Mail Removed)> 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


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-01-2012
On 01/31/2012 08:09 PM, BartC wrote:
>
>
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...

....
>> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-01-2012
"BartC" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Robert Wessel <(E-Mail Removed)> 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) (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
 
Keith Thompson
Guest
Posts: n/a
 
      02-01-2012
"Charles Richmond" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Robert Wessel <(E-Mail Removed)> 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) (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 Kuyper
Guest
Posts: n/a
 
      02-01-2012
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
 
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 Peter Nilsson C Programming 7 02-01-2012 12:15 PM



Advertisments