RE: const and volatile keywords in C

  • From: Øyvind Lode <oyvind.lode@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 20 Jul 2010 18:57:47 +0200

Here is what the author of the book I'm reading has to say about const and
volatile.
The two last examples he's listing is still unclear to me...

********************

As any programming language evolves, additional constructs are added to fill
some previously overlooked need. Two new keywords have been added to C with
the release of the ANSI-C standard. They are not illustrated in example
programs, but they will be discussed here. The two new keywords are const
and volatile and are used to tell the compiler that variables of these types
will need special consideration. A constant is declared with the const
keyword and declares a value that cannot be changed by the program. If you
inadvertently try to modify an entity defined as a const, the compiler will
generate an error. This is an indication to you that something is wrong.
Declaring an entity as const allows the optimizer to do a better job which
could make your program run a little faster. Since constants can never have
a value assigned to them in the executable part of the program, they must
always be initialized. If volatile is used, it declares a value that may be
changed by the program but it may also be changed by some outside influence
such as a clock update pulse incrementing the stored value. This prevents
the optimizer from getting too ambitious and optimizing away something that
it thinks will never be changed. 

Examples of use in declaring constants of these two types are given as; 

     const int index1 = 2;
     const index2 = 6;
     const float big_value = 126.4;
     volatile const int index3 = 12;
     volatile int index4;


-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Tyler
Littlefield
Sent: 20. juli 2010 18:43
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: const and volatile keywords in C

I'm not sure why you would use volatile and const, basically it checks the
value with volatile rather than reuse the old one from what I remember,
which is great for threading. Const doesn't lead to speed incrases
generally, just elimenates you assigning to it, which is helpful.
                Thanks,
Tyler Littlefield
        http://tds-solutions.net
        Twitter: sorressean

On Jul 20, 2010, at 10:38 AM, Øyvind Lode wrote:

> Hi all:
> 
> I know that the "const" keyword declares a value that cannot be change by
> the program and this may lead to better performance.
> If volatile is used the value may be changed by the program, but it may
also
> be changed by some outside influence.
> 
> volatile const int index3 = 12;
> volatile int index4;
> 
> The difference between the two as I see it is that the first one use both
> volatile and const and assigns 12 to an integer type to the identifier
named
> index3 and the second only uses volatile and declares an integer type
> identified as index4 but does not assign a value.
> 
> So if I do:
> 
> volatile const index3 = 12;
> volatile int index4 = 12;
> 
> Now I have two int types with two identifiers named index3 and index4 with
> the same value of 12.
> What's not clear to me is why only volatile is used and what's the
> difference between using both volatile const to only use volatile?
> 
> 
> __________
> View the list's information and change your settings at 
> //www.freelists.org/list/programmingblind
> 

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: