Keil Logo


In ANSI C, the const type qualifier is used to define and access objects that are constant and that may not be changed. A variable that is declared with const may not be assigned to in the program.

The C166 Compiler extends the ANSI definition of const objects to mean that they are stored in read-only or ROM memory. Objects you want to locate in ROM must be declared with the const type qualifier. For example:

const char test[] = "This is a text string";

Constant objects are typically initialized when they are defined (in your source files). They are placed into a CONST class which is typically located in ROM space by the linker.

The const type qualifier may be combined with the memory types near, far, and huge to specify three different CONST areas:

  • NCONST (for near constants),
  • FCONST (for far constants),
  • and HCONST (for huge constants).

The following variable definitions show different ways to create constant objects:

/* table is stored in the default memory type const class */
const int table[2][2] =
  { 0, 2, 4, 8 };

/* pi is stored in the FCONST class */
const float far pi = 3.1415927;

/* vectors is stored in the HCONST class */
const long xhuge vectors[10000] =
  { 1, 67890 };

/* mask is stored in the NCONST class */
const unsigned char near mask [] =
  { 0x01, 0x02, 0x04, 0x08 };

When using pointers to const objects, you may exclude the const type qualifier in the pointer definition. For example:

const unsigned char near mask [] =
  { 0x01, 0x02, 0x04, 0x08 };

const unsigned char near *cp = mask;
      unsigned char near  *p = mask;  /* same as cp */


*p = 'a';    // This has no effec.
             // It causes no error or warning

*cp = 'a';   // This causes an error

As shown, it is possible to assign the address of a const object (mask) to a non-const pointer (p) and subsequently use the pointer to change the const object. In this case, the compiler does generate code to write to the const object. The effects of this code is undefined and may or may not work as expected.

It is not possible to use a const pointer to change the const object it points to. Attempts to do so will cause a compiler error.

An interesting use of const is to define a pointer that may not be changed. For example:

char near text [] = "This is a string.";
char *const near textp = text;
*textp = 'A';    // This is OK (it changes text[0])
textp++;         // This causes an error (textp is const)
textp[2] = 'B';  // This is OK (it changes text[2])

In this case, the C166 Compiler locates text in the NEAR class and textp in the NCONST class. When the pointer is declared this way, it may not be changed.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.