Punctuators
The mikroC PRO for PIC punctuators (also known as separators) are:
- [ ] – Brackets
- ( ) – Parentheses
- { } – Braces
- , – Comma
- ; – Semicolon
- : – Colon
- * – Asterisk
- = – Equal sign
- # – Pound sign
Most of these punctuators also function as operators.
Brackets
Brackets [ ]
indicate single and multidimensional array subscripts:
char ch, str[] = "mikro"; int mat[3][4]; /* 3 x 4 matrix */ ch = str[3]; /* 4th element */
Parentheses
Parentheses ( )
are used to group expressions, isolate conditional expressions, and indicate function calls and function parameters:
d = c * (a + b); /* override normal precedence */ if (d == z) ++x; /* essential with conditional statement */ func(); /* function call, no args */ void func2(int n); /* function declaration with parameters */
Parentheses are recommended in macro definitions to avoid potential precedence problems during an expansion:
#define CUBE(x) ((x) * (x) * (x))
For more information, refer to Operators Precedence And Associativity and Expressions.
Braces
Braces { }
indicate the start and end of a compound statement:
if (d == z) { ++x; func(); }
Closing brace serves as a terminator for the compound statement, so a semicolon is not required after }
, except in structure declarations. Sometimes, the semicolon can be illegal, as in
if (statement) { ... }; /* illegal semicolon! */ else { ... };
For more information, refer to the Compound Statements.
Comma
Comma (,
) separates the elements of a function argument list:
void func(int n, float f, char ch);
Comma is also used as an operator in comma expressions. Mixing two uses of comma is legal, but you must use parentheses to distinguish them. Note that (exp1, exp2) evalutates both but is equal to the second:
func(i, j); /* call func with two args */ func((exp1, exp2), (exp3, exp4, exp5)); /* also calls func with two args! */
Semicolon
Semicolon (;
) is a statement terminator. Any legal 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 mikroC PRO for PIC might ignore it.
a + b; /* Evaluate a + b, but discard value */ ++a; /* Side effect on a, but discard value of ++a */ ; /* Empty expression, or a null statement */
Semicolons are sometimes used to create an empty statement:
for (i = 0; i < n; i++);
For more information, see the Statements.
Colon
Use colon (:
) to indicate the labeled statement:
start: x = 0; ... goto start;
Labels are discussed in the Labeled Statements.
Asterisk (Pointer Declaration)
Asterisk (*
) in a variable declaration denotes the creation of a pointer to a type:
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:
int **int_ptr; /* a pointer to an array of integers */ double ***double_ptr; /* a pointer to a matrix of doubles */
You can also use asterisk as an operator to either dereference a pointer or as multiplication operator:
i = *int_ptr; a = b * 3.14;
For more information, see the Pointers.
Equal Sign
Equal sign (=
) separates variable declarations from initialization lists:
int test[5] = { 1, 2, 3, 4, 5 }; int x = 5;
Equal sign is also used as an assignment operator in expressions:
int a, b, c; a = b + c;
For more information, see Assignment Operators.
Pound Sign (Preprocessor Directive)
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 a code generation. See the Preprocessor Directives for more information.
#
and ##
are also used as operators to perform token replacement and merging during the preprocessor scanning phase. See the Preprocessor Operators.
What do you think about this topic ? Send us feedback!