[comp.lang.c] Dumb question: What IS a trigraph?

2014_5001@uwovax.uwo.ca (08/16/89)

Every so often someone mentions a trigraph.  What is a trigraph???
I have programmed in C for 3 years, and the only time I met the word was when I
was reading a graph-theory book (and I cannot remember what that definition
was).

Please enlighten me.


Post or reply directly.
-- 
Alexander Pruss, at one of: Department of Applied Mathematics, Astronomy,
Mathematics, or Physics                     University of Western Ontario 
pruss@uwovax.uwo.ca         pruss@uwovax.BITNET          A5001@nve.uwo.ca 

lhf@aries5 (Luiz H. deFigueiredo) (08/16/89)

A trigraph is a 'escape sequence' to allow C sources to be written in
machines that do not have special characters like #{}.

The format of a trigraph is:

??=	meaning #
??(	meaning {
??)	meaning }

There are others but I don't remember them now. I'd have to check.
Anyway, it is not the kind of beast you'd be likely to meet...

(sorry to post but problems with mailer.)
-------------------------------------------------------------------------------
Luiz Henrique de Figueiredo		internet: lhf@aries5.waterloo.edu
Computer Systems Group			bitnet:   lhf@watcsg
University of Waterloo
			(possible domains are waterloo.edu and uwaterloo.ca)
-------------------------------------------------------------------------------

swh@hpcupt1.HP.COM (Steve Harrold) (08/17/89)

Re: Trigraphs

Poster suggests that trigraphs are not likely to be met in "everyday" C
code, but I'd like to caution you that they will show up in some very
unexpected ways.

For example, say you have a string literal in your code that reads something
like "...Are you sure you want to do this??.....".  The inncocent pairing
of the two question marks will cause the Ansi-conformant compiler to hunt
for a trigraph.  If the character immediately following the "??" completes
a legal trigraph, your string is no longer what you thought it was.

The compilation will succeed without comment, but your run-time results
may vary.

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/17/89)

In article <3566@uwovax.uwo.ca> 2014_5001@uwovax.uwo.ca writes:
>Every so often someone mentions a trigraph.  What is a trigraph???

There's one (the "???").  Standard C requires that, very early in
the phases of translation, certain sequences of three source characters
starting with "??" be mapped into equivalent single characters.  This
is intended to allow C programs to be expressed entirely using the
ISO-646 basic character set, which does not include {, |, and several
other essential C source characters.  It is not expected that real
programming environments would ever require programmers to type these
trigraph sequences; they're intended primarily for program interchange
among different sites.  Unless you're importing a lot of code from
European sites or are implementing a C compiler, you shouldn't have to
be concerned about trigraphs, since you'll never use nor see them.

bengsig@oracle.nl (Bjorn Engsig) (08/18/89)

Article <10762@smoke.BRL.MIL> by gwyn@brl.arpa (Doug Gwyn) says:
|Unless you're importing a lot of code from
|European sites or are implementing a C compiler, you shouldn't have to
|be concerned about trigraphs, ...
Well, I don't think that any Europeans use trigraphs.  We use one of the alter-
natives:

- Get a terminal like yours (i.e. US-style) and give a damm about our own
  funny characters - we never program in our own language anyway.

- Use a terminal (and software such as vi, sh, etc.) with 8-bit support

- Use nationalized terminals, where e.g. } looks like an 'a' with a small
  ring above it (try typing ComposeCharacter a * on a vt220).  You can 
  actually get used to such ugly looking C programs :-(


-- 
Bjorn Engsig, ORACLE Europe         \ /    "Hofstadter's Law:  It always takes
Path:   mcvax!orcenl!bengsig         X      longer than you expect, even if you
Domain: bengsig@oracle.nl           / \     take into account Hofstadter's Law"

mcdonald@uxe.cso.uiuc.edu (08/18/89)

>Unless you're importing a lot of code from
>European sites or are implementing a C compiler, you shouldn't have to
>be concerned about trigraphs, since you'll never use nor see them.

puts("Should I have to worry about trigraphs in ANSI C??!");

What does the above print? According to K&R I it should print

Should I have to worry about trigraphs in ANSI C??!

Does it?

Alfred E. Neumann (mcdonald@uxe.cso.uiuc.edu)

walter@hpclwjm.HP.COM (Walter Murray) (08/18/89)

Doug Gwyn writes:

>In article <3566@uwovax.uwo.ca> 2014_5001@uwovax.uwo.ca writes:
>>Every so often someone mentions a trigraph.  What is a trigraph???

>There's one (the "???").  

Nope.  Doug must not have had his dpANS handy.  The "???" smells a
little like a trigraph, but it isn't one.  Of all three-character
sequences beginning with "??", only nine are trigraphs, and "???"
isn't one of them!

Walter Murray
----------

karl@haddock.ima.isc.com (Karl Heuer) (08/19/89)

In article <10762@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
>In article <3566@uwovax.uwo.ca> 2014_5001@uwovax.uwo.ca writes:
>>What is a trigraph???
>
>There's one (the "???").

Lest anyone take that too literally, I'll point out what I'm sure Doug already
knows: although all trigraphs begin with "??", not everything beginning with
"??" is a trigraph.  In particular, "???" is not one of the nine trigraphs,
and so it would not be interpreted specially.  (Unless the next character is
one of   ( ) ! = < > / - '   in which case the first `?' is literal and the
other two are the first two-thirds of a trigraph.)

In article <5940008@hpcupt1.HP.COM> swh@hpcupt1.HP.COM (Steve Harrold) writes:
>[It's possible to accidentally create a trigraph in a string literal.]
>The compilation will succeed without comment...

Hopefully, most complete% implementations of ANSI C will warn about "possibly
unintended trigraph", especially if a trigraphable character (i.e., one of the
nine characters   [ ] | # { } \ ~ ^   ) is encountered in the same source.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
% There will undoubtedly be many "incomplete" implementations that don't
  support trigraphs.  I reserve judgement on whether this is good or bad.

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/19/89)

In article <479.nlhp3@oracle.nl> bengsig@oracle.nl (Bjorn Engsig) writes:
>Article <10762@smoke.BRL.MIL> by gwyn@brl.arpa (Doug Gwyn) says:
>|Unless you're importing a lot of code from
>|European sites or are implementing a C compiler, you shouldn't have to
>|be concerned about trigraphs, ...
>Well, I don't think that any Europeans use trigraphs.

I didn't say they did.  In the future, once there is a sufficient
desity of standard-conforming C implementations, it is possible that
C source files being transported between sites that operate in
radically different environments will often be mapped to/from trigraphs
to avoid national character-set problems during the port.

ado@elsie.UUCP (Arthur David Olson) (08/19/89)

In article <5940008@hpcupt1.HP.COM>, swh@hpcupt1.HP.COM (Steve Harrold) writes:
> . . .say you have a string literal in your code. . .like
> "...Are you sure you want to do this??.....".  The. . .two question marks
> will cause the Ansi-conformant compiler to hunt for a trigraph.
> If the character immediately following the "??" completes
> a legal trigraph, your string is no longer what you thought it was.
> 
> The compilation will succeed without comment, but your run-time results
> may vary.

	Script started on Fri Aug 18 20:26:28 1989
	elsie$ cat try.c
	#include <stdio.h>

	int
	main(argc, argv)
	int	argc;
	char *	argv[];
	{
		(void) printf("Hello, world??!\n");
		return 0;
	}
	elsie$ /usr/local/bin/gcc try.c
	elsie$ a.out
	Hello, world??!
	elsie$ /usr/local/bin/gcc -trigraphs try.c
	elsie$ a.out
	Hello, world|
	elsie$ /usr/local/bin/gcc -trigraphs -Wtrigraphs try.c
>>>>	try.c:1: warning: file contains 1 trigraph(s)
	elsie$ a.out
	Hello, world|
	elsie$ exit

	script done on Fri Aug 18 20:27:01 1989

The compilation will not *necessarily* succeed without comment.
-- 
	Arthur David Olson    ado@alw.nih.gov    ADO is a trademark of Ampex.

paul@csnz.co.nz (Paul Gillingwater) (08/20/89)

In article <3566@uwovax.uwo.ca> 2014_5001@uwovax.uwo.ca writes:
>Every so often someone mentions a trigraph.  What is a trigraph???

May I refer you to page 229 of the Kernighan and Ritchie Bible,
"The C Programming Language" (2nd. ed.).  Also, page 259 (Appendix C,
"Summary of Changes") states:
	'Trigraph sequences introduced by ?? allow representation of
characters lacking in some character sets.'

This substitution is usually performed by a preprocessor.  For instance,
the following are valid trigraphs:
	??=	#	??(	[	??<	{
	??/	\	??)	]	??>	}
	??'	^	??!	|	??-	~

>I have programmed in C for 3 years, and the only time I met the word was when I
>was reading a graph-theory book (and I cannot remember what that definition
>was).
You've programmed in C for 3 years and haven't read the Bible according
to K & R (revised ed.) ??

>Please enlighten me.
The pointer to NULL is not the whole of the dhamma.  Mu!

>Alexander Pruss, at one of: Department of Applied Mathematics, Astronomy,
-- 
Paul Gillingwater, Computer Sciences of New Zealand Limited
Bang: ..!uunet!dsiramd!csnz!paul    Domain: paul@csnz.co.nz
Call Magic Tower BBS V21/23/22/22bis 24 hrs +0064 4 767 326