4.5 – Trigraph characters in C language

by subbu on September 12, 2013

Why trigraph characters were used?

In C language we use different characters on different purposes. Say for example {} are used to define a scope, [] are used to define dimension to an array, \ is used to write any escape sequence, # is an initial character to any pre-processor statement, | is a bitwise OR, ^ is a bitwise XOR and ~ is a negation operator.

Keyboards used with some platforms were missing some of these characters. So it was not possible to write C code on these machines.

 Keyboard need trigraph characters

Trigraph characters:

  • Some of the characters like {}, [], \, |, ~ and ^ are missing in the above keyboard. Hence practically it may not be possible to write a C program using this keyboard.
  • To solve this problem C suggested to use combination of 3 characters to produce a single character called trigraph character.
  • A trigraph is a sequence of three characters, the first two of which are question marks
  • C supports the following 9 trigraph characters.
Trigraph sequence Equal character
??= #
??( [
??) ]
??/ \
??< {
??> }
??! |
??’ ^
??- ~
  • The trigraphs preprocessor replaces all occurrences of trigraph sequences by their single-character equivalents before any other processing.

We need to write a simple hello world program in the following way in the platform which doesn’t support #, {} and \ characters.

??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>

The trigraphs pre-processor changes the above code as

#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}

Trigraphs are not commonly supported by all the compilers. Some compilers support an option to turn recognition of trigraphs on. Some issues warnings when they encounter trigraphs in the source files

Executing a C program with trigraph characters in Turbo C:

Turbo C provides a trigraph preprocessor called trigraph, which accepts a C source file as input and replaces all the trigraph characters with their associated characters. In simple, trigraph preprocessor changes the source file.

The trigraph preprocessor is available in the Turbo C bin folder. Make sure that, the path is set to the bin folder of Turbo C as specified in 1.6 How to execute a C program in command prompt using Turbo C session to access the trigraph tool from any where

Turbo C trigraph

Once trigraph preprocessor replaces all the trigraph characters with their appropriate characters in the source then C program can be compiled and executed as a normal program using tcc

1. Open the text editor to write the program

>edit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

/* program to print hello world */
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>

3. Replace all the trigraph characters in the source code (prog.c) using trigraph preprocessor

>trigraph prog.c

4. We can see the replaced source file using the type command

>type prog.c
#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}

trigraph characters in Turbo C

5. Compile the program using tcc (Turbo C compiler)

>tcc prog.c

6. Execute the program

>prog
Hello
World

 Executing using tcc

Executing a C program with trigraph characters in windows:

Now we will see how a C program with trigraphs is executed using MinGw gcc compiler on windows command prompt.

1. Open the text editor to write the program

>edit hello.c

2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

/* program to print hello world */
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>

3. Compile the program using “gcc”.
Note:
-trigraphs is a compiler option to enable trigraph preprocessor
-o is a compiler option to generate executable file with file name (hello.exe)

>gcc -trigraphs -o hello hello.c

4. Execute the program

>hello
Hello
World

Executing a trigraph C program in windows

Executing a C program with trigraph characters in Linux:

Now we will see how a C program with trigraphs is executed using gcc compiler in Linux.

1. Open the gedit to write the C program

$gedit sum.c

2. Type the program, save (ctrl+s) and exit (ctrl+q)  from the editor

/* program to take two numbers and print sum */
??=include<stdio.h>
int main()
??<
int x,y;
printf("Enter two numbers:??/n");
scanf("%d%d",&x,&y);
printf("Sum %d??/n",x+y);
return 0;
??>

3. Compile the program using gcc

$gcc -trigraphs -o sum sum.c

4. Execute the program

$./sum
Enter two numbers:
45
20
Sum 65

Executing trigraphs in linux

Conclusion:

Almost all the keyboards and platforms now a days support the standard character set, hence we no need to use trigraph characters in any C program

Previous post:

Next post: