Namespaces
Variants
Views
Actions

Alternative operator representations (since C99)

From cppreference.com
< c‎ | language
Revision as of 17:34, 31 May 2013 by P12bot (Talk | contribs)

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.

Alternative spellings

There are alternative spellings for several operators defined in <iso646.h>.

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)

Digraphs and trigraphs

The following combinations of two and three characters (digraphs and trigraphs) are valid substitutions for their respective primary characters:

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

Note that trigraphs (but not digraphs) are parsed before comments and string literals are recognized, so 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:

%: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;
 
??>

Output:

(if the first command line argument to the compiled executable - argv[1] - is "monkeys")
 
Hello monkeys!