Digraph and Trigraph characters in C

Digraph Character :

The Diagraph are sequence of two character that the compiler replaces with their corresponding punctuation character.

Digraph sequence

Symbol

<%

{ left brace

%>

} right brace

<:

[ left bracket

:>

] right bracket

%:

# hash



%:include<stdio.h>
int main()
<%
    printf("Hello world!");
    return 0;
%>

which is treated the same as :-

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

Trigraph Character :

Trigraph are sequence of three characters (introduced by two consecutive Question marks ) that the compiler replaces with their corresponding punctuation characters. 

some keywords may not have all the characters from the C character set. C supports the facility of "trigraph sequence" to print these characters. These trigraph sequences have three characters. First two are '??' and third character from C character set. some trigraph sequences are as given below -

Trigraph sequence

Symbol

??<

{ left brace

??>

} right brace

??(

[ left bracket

??)

] right bracket

??!

| vertical bar

??/

\ backslash

??=

# hash

??-

~ tilde

??’

^ caret

The following source line

printf("EKD??/n");

It's saying that it  will replace the trigraph sequence, but it's not.

it's print "EKD??/n"

Now understand How trigraph work in c

Trigraphs are disabled by default in gcc. If you are using gcc then compile with -trigraphs to enable  trigraphs :

gcc -trigraphs source.c

The ??/ in the printf is a trigraph character which is a part of C's preprocessor.

If you enable trigraph by using gcc -trigaphs source.c , it will convert ??/ into \.your code will look like this :

    printf("EKD??/n"); // before enable trigraph
    printf("EKD?\n"); // after enable trigraph


??=include<stdio.h>
int main()
??<
    printf("EKD??/n"); // before enable trigraph
    return 0;
??>

which is treated the same as :-

#include<stdio.h>
int main()
{
    printf("EKD??/n"); // before enable trigraph
    return 0;
}

Comments

Popular posts from this blog

18 Amazing programs in C.