const

Defines a variable which cannot be modified by any other part in the code. You place the keyword const in front of any variable declaration. If the keyword volatile is placed after const, then this allows external routines to modify the variable (such as hardware devices). This also forces the compiler to retrieve the value of the variable each time it is referenced rather than possibly optimizing it in a register.

Constant numbers can be defined in the following way:

Constant type Description
Hexadecimal digits Where hexadecimal digits is any digit or any letter A through F or a through f.
Decimal digitsAny number where the first number is not zero
Octal constantAny number where the first number must be zero.
Floating numbers

A fractional number, optionally followed by either e or E, then the exponent. The number may be suffixed by:

  • U or u: Unsigned.
  • L or l: Long.
  • F or f: A literal of float type.
  • LL or ll: A literal of long long type.

U and L/LL can be combined.

Example

In the following example, const modifies a formal parameter of a function declaration. An actual parameter passed as i cannot be modified by the function my_itoa.

/* Function description: my_itoa converts the integer i to a string s */
    char * my_itoa( const int i, char * s) {         sprintf (s, "%d", i);         return s;     }
    int x = 5;
    char str[16];
    my_itoa(x, str);
    lr_output_message ("my_itoa returned %s", str);
Example: Output:
Action.c(8): my_itoa returned 5