volatile const

Discussion in 'C Programming' started by Rahul, Apr 10, 2008.

  1. Rahul

    Rahul Guest

    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 ! ! !
    Rahul, Apr 10, 2008
    #1
    1. Advertising

  2. Rahul wrote:
    > 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".

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Apr 10, 2008
    #2
    1. Advertising

  3. Rahul

    Richard Bos Guest

    Rahul <> wrote:

    > 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
    Richard Bos, Apr 10, 2008
    #3
  4. Rahul wrote:
    > 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
    Joachim Schmitz, Apr 10, 2008
    #4
  5. In article <ftkmd2$v35$>,
    Andrey Tarasevich <> wrote:

    >> volatile const int a = 10;


    >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
    --
    :wq
    Richard Tobin, Apr 10, 2008
    #5
  6. Rahul

    raof01 Guest

    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"
    raof01, Apr 13, 2008
    #6
    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. ben
    Replies:
    5
    Views:
    579
    Ulrich Eckhardt
    Jan 11, 2005
  2. Replies:
    11
    Views:
    1,086
  3. Javier
    Replies:
    2
    Views:
    543
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,090
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    871
    fungus
    Oct 31, 2008
Loading...

Share This Page