const modifier in c

Explanation of const modifier in c programming language by examples, questions and answers:

In c all variables are by default not constant. Hence, you can modify the value of variable by program. You can convert any variable as a constant variable by using modifier const which is keyword of c language.
Properties of constant variable:

1. You can assign the value to the constant variables only at the time of declaration. For example:

const int i=10;
float const f=0.0f;
unsigned const long double ld=3.14L;

2. Uninitialized constant variable is not cause of any compilation error. But you cannot assign any value after the declaration. For example:

const int i;

If you have declared the uninitialized variable globally then default initial value will be zero in case of integral data type and null in case of non-integral data type. If you have declared the uninitialized const variable locally then default initial value will be garbage.

3. Constant variables executes faster than not constant variables.

4. You can modify constant variable with the help of pointers. For example:

#include<stdio.h>
int main(){
    int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
    return 0;
}

Output: 20 

Function modifier in c
Interrupt modifier in c
Memory representation of double in c
Post a Comment