Namespaces
Variants
Views
Actions

Difference between revisions of "c/language/operator alternative"

From cppreference.com
< c‎ | language
m (c95)
(split digraphs from trigraphs to account for %:%:, added historical notes)
Line 6: Line 6:
 
==Alternative spellings==
 
==Alternative spellings==
  
There are alternative spellings for several operators defined in {{ttb|<iso646.h>}}.
+
===Macros===
 +
There are alternative spellings for the operators that use non-ISO646 characters, defined in {{ttb|<iso646.h>}} as macros:
  
 
{{dsc begin}}
 
{{dsc begin}}
Line 24: Line 25:
 
{{dsc end}}
 
{{dsc end}}
  
==Digraphs and trigraphs==
+
The characters {{c|&}} and {{c|!}} are invariant under ISO-646, but alternatives are provided for the operators that use these characters anyway to accommodate even more restrictive historical charsets<!-- best reference found so far "The Danish delegation did not, in fact, have a way of representing those characters on their terminals" from https://groups.google.com/d/msg/comp.std.c/eYbj0lCIvn4/89oK8U6JpqEJ , but it doesn't specifically call out & and ! -->.
  
The following combinations of two and three characters (digraphs{{mark c95}} and trigraphs{{mark|C89}}) are valid substitutions for their respective primary characters:
+
There is no alternative spelling (such as {{c|eq}}) for the equality operator {{c|{{==}}}} because the character {{c|{{=}}}} was present in all supported charsets.
 +
 
 +
===Alternative tokens{{mark c95}}===
 +
The following alternative tokens are part of the core language, and, in all respects of the language, each alternative token behaves exactly the same as its primary token, except for its spelling (the [[c/preprocessor/replace|stringification operator]] can make the spelling visible). The two-letter alternative tokens are sometimes called "digraphs"
 +
 
 +
 
 +
{| class="wikitable"
 +
|- style="text-align: left;"
 +
! Primary
 +
! Alternative
 +
|-
 +
| {{tt|{}} || {{tt|<%}}
 +
|-
 +
| {{tt|}}} || {{tt|%>}}
 +
|-
 +
| {{tt|[}} || {{tt|<:}}
 +
|-
 +
| {{tt|]}} || {{tt|:>}}
 +
|-
 +
| {{tt|#}} || {{tt|%:}}
 +
|-
 +
| {{tt|##}} || {{tt|%:%:}}
 +
|}
 +
 
 +
==Trigraphs==
 +
 
 +
The following three-character groups (trigraphs) are {{rlp|translation_phases|parsed before comments and string literals are recognized}}, and each appearance of a trigraph is replaced by the corresponding primary character:
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|- style="text-align: left;"
 
|- style="text-align: left;"
 
! Primary
 
! Primary
! Digraph
 
 
! Trigraph
 
! Trigraph
 
|-
 
|-
| {{tt|{}} || {{tt|<%}} || {{tt|??<}}
+
| {{tt|{}} || {{tt|??<}}
 
|-
 
|-
| {{tt|}}} || {{tt|%>}} || {{tt|??>}}
+
| {{tt|}}} || {{tt|??>}}
 
|-
 
|-
| {{tt|[}} || {{tt|<:}} || {{tt|??(}}
+
| {{tt|[}} || {{tt|??(}}
 
|-
 
|-
| {{tt|]}} || {{tt|:>}} || {{tt|??)}}
+
| {{tt|]}} || {{tt|??)}}
 
|-
 
|-
| {{tt|#}} || {{tt|%:}} || {{tt|??{{=}}}}
+
| {{tt|#}} || {{tt|??{{=}}}}
 
|-
 
|-
| {{tt|\}} || || {{tt|??/}}
+
| {{tt|\}} || {{tt|??/}}
 
|-
 
|-
| {{tt|^}} || || {{tt|??'}}
+
| {{tt|^}} || {{tt|??'}}
 
|-
 
|-
| {{tt|<nowiki>|</nowiki>}} || || {{tt|??!}}
+
| {{tt|<nowiki>|</nowiki>}} || {{tt|??!}}
 
|-
 
|-
| {{tt|~}} || || {{tt|??-}}
+
| {{tt|~}} || {{tt|??-}}
 
|}
 
|}
  
Note that trigraphs (but not digraphs) are {{rlp|translation_phases|parsed before comments and string literals are recognized}}, so a comment such as {{c|// Will the next line be executed?????/}} will effectively comment out the following line, and the string literal such as {{c|"What's going on??!"}} is parsed as {{c|"What's going on{{!}}"}}.
+
Because trigraphs are processed early, a comment such as {{c|// Will the next line be executed?????/}} will effectively comment out the following line, and the string literal such as {{c|"What's going on??!"}} is parsed as {{c|"What's going on{{!}}"}}.
  
 
===Example===
 
===Example===

Revision as of 07:28, 8 December 2016

C source code may be written in any non-ASCII 7-bit character set that includes the ISO 646:1983 invariant character set. However, several C operators and punctuators require characters that are outside of the ISO 646 codeset: {, }, [, ], #, \, ^, |, ~. To be able to use character encodings where some or all of these symbols do not exist (such as the German DIN 66003), there are two possibilities: alternative spellings of operators that use these characters or special combinations of two or three ISO 646 compatible characters that are interpreted as if they were a single non-ISO 646 character.

Contents

Alternative spellings

Macros

There are alternative spellings for the operators that use non-ISO646 characters, defined in <iso646.h> as macros:

Defined in header <iso646.h>
Primary Alternative
&&
and
(macro constant)
&=
and_eq
(macro constant)
&
bitand
(macro constant)
|
bitor
(macro constant)
~
compl
(macro constant)
!
not
(macro constant)
!=
not_eq
(macro constant)
||
or
(macro constant)
|=
or_eq
(macro constant)
^
xor
(macro constant)
^=
xor_eq
(macro constant)

The characters & and ! are invariant under ISO-646, but alternatives are provided for the operators that use these characters anyway to accommodate even more restrictive historical charsets.

There is no alternative spelling (such as eq) for the equality operator == because the character = was present in all supported charsets.

Alternative tokens(C95)

The following alternative tokens are part of the core language, and, in all respects of the language, each alternative token behaves exactly the same as its primary token, except for its spelling (the stringification operator can make the spelling visible). The two-letter alternative tokens are sometimes called "digraphs"


Primary Alternative
{ <%
} %>
[ <:
] :>
# %:
## %:%:

Trigraphs

The following three-character groups (trigraphs) are parsed before comments and string literals are recognized, and each appearance of a trigraph is replaced by the corresponding primary character:

Primary Trigraph
{ ??<
} ??>
[ ??(
] ??)
# ??=
\ ??/
^ ??'
| ??!
~ ??-

Because trigraphs are processed early, a comment such as // Will the next line be executed?????/ will effectively comment out the following line, and the string literal such as "What's going on??!" is parsed as "What's going on|".

Example

The following example demonstrates alternative operator spellings from the <iso646.h> header as well as use of digraphs and trigraphs. The space character in the first command-line argument, argv[1], requires the quotation marks: ", World!".

%:include <stdlib.h>
%:include <stdio.h>
%:include <iso646.h>
 
int main(int argc, char** argv)
??<
    if (argc > 1 and argv<:1:> not_eq NULL)
    <%
       printf("Hello%s\n", argv<:1:>);
    %>
 
    return EXIT_SUCCESS;
??>

Possible output:

Hello, World!
C++ documentation for Alternative operator representations