changing const variables

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Feb 22, 2008.

  1. Guest

    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 ...
     
    , Feb 22, 2008
    #1
    1. Advertising

  2. santosh Guest

    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?
     
    santosh, Feb 22, 2008
    #2
    1. Advertising

  3. Richard Bos Guest

    santosh <> wrote:

    > 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
     
    Richard Bos, Feb 22, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,143
  2. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,157
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    923
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    552
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page