Home » Blog » Can a variable be both const and volatile?

Can a variable be both const and volatile?

yes, the const means that the variable cannot be assigned a new value. The value can be changed by other code or pointer. For example the following program works fine.

int main(void) 
{ 
    const volatile int local = 10; 
    int *ptr = (int*) &local;  
    printf("Initial value of local : %d \n", local);  
    *ptr = 100;  
    printf("Modified value of local: %d \n", local);  
    return 0; 
}

 

Leave a Reply

Your email address will not be published.