RAD Studio |  |

Punctuators

The C++ punctuators (also known as separators) are:

- [ ]
- ( )
- { }
- ,
- ;
- :
- ...

- =
- #

Most of these punctuators also function as operators.

Brackets

Open and close brackets indicate single and multidimensional array subscripts:

Copy Code

    char ch, str[] = "Stan";
    int mat[3][4];           /* 3 x 4 matrix */
    ch = str[3];             /* 4th element */
      .
      .
      .

Parentheses

Open and close parentheses ( ) are used to group expressions, isolate conditional expressions, and indicate function calls and function parameters:

Copy Code

    d = c * (a + b);/* override normal precedence */
    if (d == z) ++x;/* essential with conditional statement */
    func();/* function call, no args */
    int (*fptr)();/* function pointer declaration */
    fptr = func;/* no () means func pointer */
    void func2(int n);/* function declaration with parameters */

Parentheses are recommended in macro definitions to avoid potential precedence problems during expansion:

Copy Code

    #define CUBE(x) ((x) * (x) * (x))

The use of parentheses to alter the normal operator precedence and associativity rules is covered in Expressions.

Braces

Open and close braces { } indicate the start and end of a compound statement:

Copy Code

    if (d == z)
    {
       ++x;
       func();
    }

The closing brace serves as a terminator for the compound statement, so a ; (semicolon) is not required after the }, except in structure or class declarations. Often, the semicolon is illegal, as in

Copy Code

    if (statement)
       {};                  /* illegal semicolon */
    else

Comma

The comma (,) separates the elements of a function argument list:

Copy Code

    void func(int n, float f, char ch);

The comma is also used as an operator in comma expressions. Mixing the two uses of comma is legal, but you must use parentheses to distinguish them. Note that (exp1, exp2) evaluates both but is equal to the second:

Copy Code

    func(i, j);                              /* call func with two args */
    func((exp1, exp2), (exp3, exp4, exp5));  /* also calls func with two args! */

Semicolon

The semicolon (;) is a statement terminator. Any legal C or C++ expression (including the empty expression) followed by a semicolon is interpreted as a statement, known as an expression statement. The expression is evaluated and its value is discarded. If the expression statement has no side effects, the compiler might ignore it.

Copy Code

    a + b;    /* maybe evaluate a + b, but discard value */
    ++a;      /* side effect on a, but discard value of ++a */
    ;         /* empty expression = null statement */

Semicolons are often used to create an empty statement:

Copy Code

    for (i = 0; i < n; i++)
    {
       ;
    }

Colon

Use the colon (:) to indicate a labeled statement:

Copy Code

    start:    x=0;
    ...
    goto start;

Labels are discussed in Labeled statements.

Ellipsis

The ellipsis (...) is three successive periods with no intervening whitespace. Ellipses are used in the formal argument lists of function prototypes to indicate a variable number of arguments, or arguments with varying types:

Copy Code

    void func(int n, char ch,...);

This declaration indicates that func will be defined in such a way that calls must have at least two arguments, an int and a char, but can also have any number of additional arguments.

In C++, you can omit the comma before the ellipsis.

Asterisk (pointer declaration)

The asterisk (*) in a variable declaration denotes the creation of a pointer to a type:

Copy Code

    char *char_ptr;  /* a pointer to char is declared */

Pointers with multiple levels of indirection can be declared by indicating a pertinent number of asterisks:

Copy Code

    int **int_ptr;         /* a pointer to an integer array */
    double ***double_ptr;  /* a pointer to a matrix of doubles */

You can also use the asterisk as an operator to either dereference a pointer or as the multiplication operator:

Copy Code

    i = *int_ptr;
    a = b * 3.14;

Equal sign (initializer)

The equal sign (=) separates variable declarations from initialization lists:

Copy Code

    char array[5] = { 1, 2, 3, 4, 5 };
    int  x = 5;

In C++, declarations of any type can appear (with some restrictions) at any point within the code. In a C function, no code can precede any variable declarations.

In a C++ function argument list, the equal sign indicates the default value for a parameter:

Copy Code

    int f(int i = 0) { ... }  /* Parameter i has default value of zero */

The equal sign is also used as the assignment operator in expressions:

Copy Code

    int a, b, c;
    a = b + c;
    float *ptr = (float *) malloc(sizeof(float) * 100);

Pound sign (preprocessor directive)

The pound sign (#) indicates a preprocessor directive when it occurs as the first nonwhitespace character on a line. It signifies a compiler action, not necessarily associated with code generation. See Preprocessor directives for more on the preprocessor directives.

# and ## (double pound signs) are also used as operators to perform token replacement and merging during the preprocessor scanning phase. See Token pasting.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!