changing const variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    changing const variables

    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 ...


  • santosh

    #2
    Re: changing const variables

    junky_fellow@ya hoo.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?

    Comment

    • Richard Bos

      #3
      Re: changing const variables

      santosh <santosh.k83@gm ail.comwrote:
      junky_fellow@ya hoo.co.in wrote:
      >
      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.
      He has been asking simple questions for years. He has not been learning.

      Richard

      Comment

      Working...