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

9 comments:

robo said...

in the last example i is not a constant its a variable because there is no prefix const before i

Unknown said...

inter view Question
const int a;
int const a;
const int *a;
int * const a;
int const * a const;

The first two mean the same thing, namely a is a const (read-only) integer. The third means a is
a pointer to a const integer (that is, the integer isn't modifiable, but the pointer is). The fourth
declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable,
but the pointer is not). The final declaration declares a to be a const pointer to a const integer
(that is, neither the integer pointed to by a, nor the pointer itself may be modified). If the
candidate correctly answers these questions, I'll be impressed. Incidentally, you might wonder
why I put so much emphasis on const, since it is easy to write a correctly functioning program
without ever using it. I have several reasons:
The use of const conveys some very useful information to someone reading your code. In
effect, declaring a parameter const tells the user about its intended usage. If you spend a
lot of time cleaning up the mess left by other people, you'll quickly learn to appreciate
this extra piece of information. (Of course, programmers who use const , rarely leave a
mess for others to clean up.)
const has the potential for generating tighter code by giving the optimizer some
additional information.
Code that uses const liberally is inherently protected by the compiler against inadvertent
coding constructs that result in parameters being changed that should not be. In short,

Unknown said...

I executed this prog in c...it showed error saying ; cannot convert 'int *' to 'int' in Turbo c++ version 3

Unknown said...

IT should be *ptr=(int)20; the only output becomes 20

Unknown said...

even no need to use typecasting in turbo c++ version 3.0
and do..like *ptr=20; then also we get ans. 20.

Unknown said...

is pointers modifier in C is removed???
what about following link??
Function modifier in c(BLUE)
Interrupt modifier in c (BLUE)
Memory representation of double in c (BLUE)

Faizal said...

The Topics and Contents are simple Good...It's very clear to Understand the Concepts. The information's about Best JAVA Training is Good...Excellent Work!!!
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

KBC Lottery Winner said...

KBC is india's Most awaiting TV Show to give you chance to win bumper cash prizes in KBC Show, Register in KBC Lottery Winner 2020 and Win extra cash to change your fate. Its all about KBC.

Chetan said...

pointer modifier, function modifier, interrupt modifier link is not active? please help