Trigraphs In C++ - C++ Programming Tutorial
C++ Course / Data Structures / Trigraphs In C++

Trigraphs In C++

BLUF: Mastering Trigraphs In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Trigraphs In C++

C++ is renowned for its efficiency. Learn how Trigraphs In C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

When converting C and C++ source code, a series of three-character combinations called trigraphs are replaced with individual characters. This feature was introduced into the C standard in 1990 to enable programming in character sets lacking certain essential C language characters.

Regardless of the character set used in the source code, trigraphs are consistently substituted with the corresponding character. This ensures that any platform capable of running C or C++ programs can execute code containing trigraphs.

The nine trigraph sequences are detailed in the table below, along with the corresponding characters that substitute them:

S.No. Trigraph Equivalent
1. ??= #
2. ??/ \
3. ??' ^
4. ??( [
5. ??) ]
6. ??!
7. ??_PRESERVE5__ }
9. ??- ~

Syntax:

Example

??=define
Becomes,
#define

Throughout the translation phase of the compilation process, trigraphs are exchanged with their corresponding characters, rendering them undetectable within the resulting binary code.

The utilization of trigraphs in the C and C++ programming languages is a topic of debate. Some individuals view trigraphs as a perplexing and unnecessary aspect, whereas others consider them a beneficial resource for portability.

Trigraphs can be employed or omitted in C or C++ programs based on individual preferences. Using trigraphs can be advantageous when developing code intended for compilation on various platforms to ensure its cross-platform functionality. However, it's important to note that not all compilers support trigraphs. Therefore, if you are targeting a particular platform, you may opt to refrain from their usage.

C++ Trigraph Example:

The fundamental Trigraphs sequence demonstration is presented below:

Example

// C++ implementation to illustrate the example of trigraph sequences

# include <iostream>
using namespace std;

??=define MSG "JavaCppTutorial"
??=define Program "C++"

// Driver Code
int main()
??< // Here ??< denotes {
cout<< "My message to " << MSG <<endl;
cout<< "My program is " << Program;

    return 0;
??> // Here ??>denotes }

Output:

Output

My message to JavaCppTutorial
My program is C++

Another example of a trigraph sequence is shown below:

Example

// C++ program to demonstrate
// the example
// of trigraph sequences

# include <iostream>
using namespace std;

// Here ??= denotes #
??=define Name "saswat"
??=define Age "26"
??=define Qualification "MCA"
??=define From "Odisha"

// Driver Code
int main()
??<
	cout<< "My Name is: " << Name <<endl;
	cout<< "My Age is: " << Age <<endl;
	

	cout<< "My Qualification is: "
		<< Qualification <<endl;
	cout<< "I'm From " << From;
	
	return 0;
??>

Output:

Output

My Name is: saswat
My Age is: 26
My Qualification is: MCA
I'm From odisha.

Difficulties with Trigraphs

Trigraphs come with several limitations. One of the downsides is their restricted capacity to substitute a specific set of characters. Another disadvantage is the complexity and difficulty in comprehending trigraphs. Lastly, not all compilers are compatible with trigraphs.

Solutions for Trigraphs

Trigraph sequences can be substituted with different arrangements. One approach is to employ a character set containing all the necessary symbols for C or C++ programming languages. Another method involves creating macros for the characters not present in the character set through a preprocessor directive. Lastly, you have the choice to utilize a tool to convert your code into a character set encompassing all the required characters.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience