C Programming Language — Quick Reference

Comments

// Single-line comment.

/* Multi-line
comment. */

Commands

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 .field structure field accessors.

Expressions

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

Control flow

Goto
 label:
  
  goto label;
If Switch
if (expression) {
  
}
else if (expression) {
  
}

else {
  
}
           
switch (expression) {
case integer:
  
  break;



default:
  
  break;
}

The else if and else blocks in if are optional, as is the default case in switch.

Multiple case integer: labels may be attached to a single case in 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 break; to break out of a switch, while, or for block.

You can use continue; to skip the rest of the current iteration of a while or for loop.

Defining variables

// 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.
  
}

Defining global constants

// Definition
const Type Name = value;

// Definition for value of structure type
const Type Name = { value1,  valuen };

Defining procedures

// Definition
Type Name(Type1 parameter1,  Typen parametern)
{
  
}

You can use a return type of void for a procedure that returns nothing, or noreturn void for a procedure that does not return at all.

You can use return expression; to return from a procedure with non-void return type, or return; to return from a procedure with void return type.

Defining data types

Alias
typedef Type Name;
Enumeration Structure
typedef Int32 Name;
enum {
  Tag1,
  
  Tagn
};
           
typedef struct {
  Type field;
  
} Name;

Conventions used in Biksel

Naming

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).

Modes and formats

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.

Gotchas to 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.

(+) Equals

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)
{
  
}

(−) Labels

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:;
}

(+) Fall through

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:

}

(+) Variables in switch

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:

}