ju**********@yahoo.co.in wrote:
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?