16

I'm curious about every context in which a colon (the ":" character) is a valid syntactic element (outside of a string/character literal, comment, etc) in a C program.

I tried searching C99 spec, but ":" matches every single page and "colon" doesn't find every usage. Similarly, by looking through toy C parsers (and I understand that lex/yacc aren't capable of parsing C) I only seem to find partial results.

These are the scenarios that I know use a colon:

  • Conditional operator
  • Bit field
  • Labels

Are there any other language features in C that use a colon?

4
  • What do you mean by "Definition of struct or union"? Other than bitfields, I can't think of any place you'd use a colon in a struct or union definition. There's a lot of them in C++ but not in C. If you want preprocessor stuff too, there are some digraphs with colons. Moving on to extensions, there's GNU inline asm with constraints set off by colons.
    – user2404501
    Jun 4, 2016 at 22:37
  • @WumpusQ.Wumbley You're right, I was combining notes from various places and accidentally duplicated them. I removed one of the references. Jun 4, 2016 at 22:40
  • Just out of curiosity: Why do you need to know this? And why the colon, not the dot, the hyphen, not the whatever?-S
    – alk
    Jun 5, 2016 at 11:18
  • @alk I was trying to understand old IOCCC winners and got tripped up on a stray colon. Eventually I found that it was the second half of a conditional operator (and that normal precedence rules get modified in the middle portion of a ternary operator), but I was still curious about other applications of the relatively obscure character. Jun 5, 2016 at 23:18

4 Answers 4

13
+50

The C standard (N1570) defines digraphs:

6.4.6 Punctuators
....

3   In all aspects of the language, the six tokens

        <: :> <% %> %: %:%:

  behave, respectively, the same as the six tokens 79)

        [ ] { } # ##

except for their spelling.80)


79) These tokens are sometimes called ‘‘digraphs’’.

80) Thus [ and <: behave differently when ‘‘stringized’’ (see 6.10.3.2), but can otherwise be freely interchanged.

As a side note, C++ standard elaborates on the term:

The term “digraph” (token consisting of two characters) is not perfectly descriptive, since one of the alternative preprocessing-tokens is %:%: and of course several primary tokens contain two characters. Nonetheless, those alternative tokens that aren’t lexical keywords are colloquially known as “digraphs”.

According to Digraphs and trigraphs:

In 1994 a normative amendment to the C standard, included in C99, supplied digraphs as more readable alternatives to five of the trigraphs. ....

Unlike trigraphs, digraphs are handled during tokenization, and any digraph must always represent a full token by itself, or compose the token %:%: replacing the preprocessor concatenation token ##. If a digraph sequence occurs inside another token, for example a quoted string, or a character constant, it will not be replaced.

1
  • 2
    Wow. I had absolutely no idea. Jun 4, 2016 at 22:31
7

Searching the C99 Appendix A grammar, we have...

punctuators

Already covered by @AlexD.

conditional-expression (aka ternary)

logical-OR-expression ? expression : conditional-expression

struct-declarator (aka struct bit-fields)

declarator[optional] : constant-expression

labeled-statement (most often used in switch statements)

identifier : statement
case constant-expression : statement
default : statement

And that's it.

7

In addition to the cases already mentioned, the : character may legally appear in (references are to the N1570 draft of the C11 standard, syntax in 6.10p1):

  • The h-char-sequence of a #include directive:
    #include <foo:bar.h>
    (6.10.2p2)

  • The q-char-sequence of a #include or #line directive:
    #include "foo:bar.h"
    This is not syntactically a string literal (6.10.2p3)

  • The replacement-list of a macro definition:
    #define COLON :

  • A non-directive (which, in spite of the name, is actually a preprocessor directive:
    # :
    Yes, I believe this is valid, though gcc and clang reject it.

  • A #error directive:
    #error foo : bar

  • A #pragma directive: #pragma foo : bar

None of these are likely to occur in real code (though I suppose a #include directive for Windows-specific code might refer to "C:\dir\blah.h").

7

Since tag is , not e.g. , I encountered not yet mentioned in answers usage in GNU C extension asm extended:

With extended asm you can read and write C variables from assembler and perform jumps from assembler code to C labels. Extended asm syntax uses colons (:) to delimit the operand parameters after the assembler template:

 asm [volatile] ( AssemblerTemplate
                  : OutputOperands
                  [ : InputOperands
                  [ : Clobbers ] ])

 asm [volatile] goto ( AssemblerTemplate
                       :
                       : InputOperands
                       : Clobbers
                       : GotoLabels)

The asm keyword is a GNU extension. When writing code that can be compiled with -ansi and the various -std options, use __asm__ instead of asm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.