Notes on the const qualifier in C

Synopsis

const <variable declaration>

Variables that are defined as constant are expected to not have their rvalue modified. If you try and modify a constant’s rvalue during program execution the compiler might issue a warning (although it is not required to do so). One of the motivations for defining a variable as constant (in addition to added readability) is that it allows the compiler to place the constant variables into read-only memory.

Examples:

const int theNumberFive = 5;
const char myName[] = "Jon";
const double theNumberSix = 6.;

int main(int argc, const char * argv[])
{
    printf("A number is: %i\n", theNumberFive);
    printf("My name is: %s\n", myName);
    printf("Another number is: %f\n", theNumberSix);
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *