changing const variables

J

junky_fellow

guys,

If I declare a const variable and then try to change it as follows;
const int i=5;
i = 10;

What would be the behaviour? Should compiler give compilation error or
Warning ?

Or, would I get a run time error (some exception as I am trying to
change a
const variable) ?

can compiler put const variables in read only memory ?

I tried this on multiple platforms using different compiler and I am
getting
different results, that is why I am asking this here.

I want to know the correct behaviour as per the standard C.

Again, thanks a lot for any help ...
 
S

santosh

guys,

If I declare a const variable and then try to change it as follows;
const int i=5;
i = 10;

What would be the behaviour? Should compiler give compilation error or
Warning ?

Haven't you been learning C long enough to know this? You have almost
certainly asked similar questions before and got answered.
Or, would I get a run time error (some exception as I am trying to
change a const variable) ?

C has no exceptions. However a runtime error is possible.
can compiler put const variables in read only memory ?

What does your common sense tell you? What is the reason for the
existence of read-only memory? To store read-only values right? And
const in C indicates read-only values right? Can you connect the dots
now?

But be aware that read-only memory may not be readily available to all C
implementations and some implementations may not take advantage of such
storage, even if they are available.
I tried this on multiple platforms using different compiler and I am
getting different results, that is why I am asking this here.

The const qualifier indicates that the object's value should not be
modified after initialisation. Doing so is undefined behaviour. This is
what the Standard says. The actual behaviour under different systems is
certainly likely to vary from seeming to work to terminating
immediately. Regardless of the program's behaviour *after* a const
object is written to, (which can be virtually anything since it's
undefined by the Standard), the fact remains that *you* , the
programmer, *should* *not* modify a const qualified object.
I want to know the correct behaviour as per the standard C.

It's formally undefined and almost certainly a programming mistake.
Again, thanks a lot for any help ...

How long do you plan to keep asking FAQs?
 
R

Richard Bos

santosh said:
Haven't you been learning C long enough to know this? You have almost
certainly asked similar questions before and got answered.

He has been asking simple questions for years. He has not been learning.

Richard
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top