volatile const

R

Rahul

Hi Everyone,

The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?

Thanks in advance ! ! !
 
A

Andrey Tarasevich

Rahul said:
The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?

Why should it be an error? This is exactly what 'volatile const' is
supposed to mean in the first place: the program is not allowed to
change the object, but its value can change "by itself".
 
R

Richard Bos

Rahul said:
int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant

No, it's being declared as something _you_ won't change.
and at the same time indicating that its value could change outside
the program's control...

Quite. It is something your code won't change, but something else will.
One example of this could be a hardware counter, or an input port.

Richard
 
J

Joachim Schmitz

Rahul said:
Hi Everyone,

The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?

Because for some stupid reason in C const doesn't mean 'constant' but 'read
only'

Bye,Jojo
 
R

Richard Tobin

volatile const int a = 10;
[/QUOTE]
Why should it be an error? This is exactly what 'volatile const' is
supposed to mean in the first place: the program is not allowed to
change the object, but its value can change "by itself".

The standard gives this example:

extern const volatile int real_time_clock;

You can't change the time, but it changes anyway.

-- Richard
 
R

raof01

Hello Joachim,
Because for some stupid reason in C const doesn't mean 'constant' but
'read only'

Yes, absolutely. Furthermore, "const" means read only JUST for you. On the
other hand, "volatile" doesn't mean you can modify it but you HAVE TO reload
it from memory every time you use it. "volatile" is more like an indicator
for compiler to protect from optimizing.

raof01
"Thoughts are but dreams till their effects be tried. -- William Shakespeare"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top