// Single-line comment. /* Multi-line comment. */
Procedure(expression1,
expressionn); | procedure call |
---|---|
variable++;
| increment |
variable--;
| decrement |
variable = expression;
| set value |
variable +=
expression;
| add |
variable -=
expression;
| subtract |
variable *=
expression;
| multiply by |
variable /=
expression;
| divide by |
variable %=
expression;
| remainder on division by |
In the above, variable
may be followed by
any number of
structure
field accessors.
.
field
Numbers, text in double quotes, variables, constants, and enumeration tags are expressions.
The following are operator expressions, listed in decreasing order of precedence with operators of equal precedence grouped together:
Procedure(expression1,
expressionn) | procedure call |
---|---|
expression.field
| structure field access |
-expression
| unary minus |
!expression
| logical NOT |
(Type) expression
| type cast |
expression *
expression
| times |
expression /
expression
| divide |
expression %
expression
| remainder |
expression +
expression
| plus |
expression -
expression
| minus |
expression <
expression
| less than |
expression <=
expression
| less than or equals |
expression >
expression
| greater than |
expression >=
expression
| greater than or equals |
expression ==
expression
| equals |
expression !=
expression
| not equals |
expression &&
expression
| logical AND |
expression ||
expression
| logical OR |
expression ? expression :
expression
| if-then-else |
Goto |
---|
label:
goto label;
|
If | Switch | |
---|---|---|
if (expression) {} else if (expression) { } else { } |
|
switch (expression) { case integer:break; default: break; } |
The
and else if
blocks in else
if
are optional, as is
the
case in default
switch
.
Multiple
labels may be attached to a single case in case integer:
switch
.
While | For | |
---|---|---|
while (expression) {
}
|
|
for (command1; expression; command2) {
}
|
A for
loop is similar to a while
loop, with the added feature that command1
is executed before the loop starts
and command2 is executed after each
iteration of the loop.
Infinite loop | Repeat n times | |
---|---|---|
while (True) {
}
|
|
Int32 i;for (i = 0; i < n; i++) { } |
You can use
to break out of
a break;
switch
, while
, or for
block.
You can use
to skip the rest of
the current iteration of a continue;
while
or for
loop.
// Uninitialized variable
Type name;
// Initialized variable
Type name = expression;
// Constant
const Type name = expression;
// Block
{
// Variables defined here exist until the end of the block.
}
// Definition
const Type Name = value;
// Definition for value of structure type
const Type Name = { value1,
valuen };
// Definition Type Name(Type1 parameter1,Typen parametern) { }
You can use a return type of
for a
procedure that returns nothing, or void
for a procedure that does not return at all.
noreturn
void
You can use
to
return from a procedure with non-return expression;
void
return type,
or
to return from a procedure with
return;
void
return type.
Alias |
---|
typedef Type Name; |
Enumeration | Structure | |
---|---|---|
typedef Int32 Name;
enum {
Tag1,
Tagn
};
|
|
typedef struct {
Type field;
} Name;
|
Write all names in camel case
(i.e. thisIsCamelCase
).
Use lower case for the first letter of names that have local scope (labels, variables, local constants, and structure fields) and use upper case for the first letter of names that have global scope (global constants, procedures, data types, and enumeration tags).
When you define a procedure, write
a mode
declaration in a comment for each parameter of
type Channel
.
When you create a memory file that will have a particular format, write a file format declaration in a comment.
Gotchasto watch out for
No programming language is perfect. C has a few aspects to its design that result in common programming pitfalls. These pitfalls come in two categories: (+) situations where the C compiler accepts code that arguably it should reject, and (−) situations where the C compiler rejects code that arguably it should accept.
Beware of accidentally setting the value of a variable when you mean to test for equality:
// WRONG but accepted by compiler
if (x = 3)
{
}
|
|
// CORRECT
if (x == 3)
{
}
|
C does not allow a label to be followed immediately by a variable definition or by the closing brace of a block. To work around this you can place a semicolon after the label:
// Rejected by compiler label: Type variable = expression;// Rejected by compiler { label: } |
|
// CORRECT label:; Type variable = expression;// CORRECT { label:; } |
If you forget to break at the end of a case in
a switch
block, control will fall through to the
next case:
// Often not what you intended switch (expression) {case integer: // no break case integer: } |
|
// CORRECT switch (expression) {case integer: break; case integer: } |
If you define any variables within a case in
a switch
block, it is recommended to put the code
for that case in a sub-block of its own. This ensures that the
variables will not remain in scope in subsequent cases, which can
lead to unexpected behaviour:
// WRONG: variable still in // scope after end of case switch (expression) {case integer: Type variable = expression; break; case integer: } |
|
// CORRECT switch (expression) {case integer: { Type variable = expression; } break; case integer: } |