cv (const-volatility) specifiers and qualifiers

From cppreference.com
< c‎ | language

[edit] Keywords

const, volatile

[edit] Example

The keyword "const" turns a variable into a constant. The constant must be initialized with a value at definition but not assigned a value later. In this example, the compiler errors are as instructive as the clean code.

#include <stdio.h>
 
int main(void)
{
    /* The pointer, the target of the pointer, or both may be const. */
 
    int i = 0;                                   /* A VARIABLE */
    int * ptr                            = &i;
    int const * ptr_to_const             = &i;
    int * const const_ptr                = &i;
    int const * const const_ptr_to_const = &i;
    const int* const c_p_t_c             = &i;   /* also valid */
 
    *ptr = 1;                   /* AOK: modifies the target       */
    ptr  = NULL;                /* AOK: modifies the pointer      */
    *ptr_to_const = 0;          /* NOK: cannot modify the target  */
    ptr_to_const  = NULL;       /* AOK: modifies the pointer      */
    *const_ptr = 0;             /* AOK: modifies the target       */
    const_ptr  = NULL;          /* NOK: cannot modify the pointer */
    *const_ptr_to_const = 0;    /* NOK: cannot modify the target  */
    const_ptr_to_const  = NULL; /* NOK: cannot modify the pointer */
 
    const int j = 0;                             /* A CONSTANT               */
    int const jj = 0;                            /* also valid               */
    int * j_ptr                            = &j; /* NOK: int* <== const int* */
    int const * j_ptr_to_const             = &j;
    int * const j_const_ptr                = &j; /* NOK: int* <== const int* */
    int const * const j_const_ptr_to_const = &j;
 
    *j_ptr = 1;                   /* AOK: modifies the target       */
    j_ptr  = NULL;                /* AOK: modifies the pointer      */
    *j_ptr_to_const = 0;          /* NOK: cannot modify the target  */
    j_ptr_to_const  = NULL;       /* AOK: modifies the pointer      */
    *j_const_ptr = 0;             /* AOK: modifies the target       */
    j_const_ptr  = NULL;          /* NOK: cannot modify the pointer */
    *j_const_ptr_to_const = 0;    /* NOK: cannot modify the target  */
    j_const_ptr_to_const  = NULL; /* NOK: cannot modify the pointer */
 
    return 0;
}

Possible output:

(none)

[edit] Example

The keyword "volatile" prevents the compiler from optimizing out the do-nothing code of function f, and the run takes about 3 seconds. Removing the keyword allows optimizing the double for loop, and the function really has nothing to do.

#include <stdio.h>
#include <time.h>
 
/* Function f() performs some time-consuming work. */
void f()
{
    /* Keyword "volatile" tells the compiler not to assume that this function */
    /* is the only part of the system that could change variable d.           */
    volatile double d;
    int m,n;
    for (n=0; n<10000; ++n)
       for (m=0; m<10000; ++m)
           d += d*n*m;
}
 
int main(void)
{
    int i;
    clock_t t = clock();
    for (i=0;i<3;++i) f();
    t = clock() - t;
    printf("Time used: %f seconds\n", ((float)t)/CLOCKS_PER_SEC);
    return 0;
}

Possible output:

Time used: 3.040000 seconds