Thread: Const and volatile

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Const and volatile

    If I use const qualifier the program cannot modify it.

    If I use volatile qualifier it can be modified by external effects. But what about internal effects? Can I modify it with a statement in the program.

    I may try it with my compiler but unfortunately I don't know how to use volatile. Do you have a code for volatile that changes with external effects?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here's a thought.... why don't you give it a try... see if you can modify a volitile variable by internal events... then drop back in and let us know how you made out...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it's volatile, of course you can modify it.

    If it's const volatile, of course you can't modify it.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    I have given it a try:

    Code:
    #include <stdio.h>
    
    main()
    {
      volatile int a=1;
      a=a+5;
      printf("a=%d\n",a);
      return 0;
    }
    Then it worked. But what is the difference between "volatile int a=1" and "int a=1"?

    When do I need to write volatile?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When the variable represents a volatile quantity. (When it can be changed from something else, whether it's a physical device, or even another thread in the same program.)

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by tabstop View Post
    When the variable represents a volatile quantity. (When it can be changed from something else, whether it's a physical device, or even another thread in the same program.)
    If I write only "int a" for something that can be changed by a physical device, doesn't it work?

    And where can I find such a code? I want to give tries on it.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GokhanK View Post
    If I write only "int a" for something that can be changed by a physical device, doesn't it work?
    Probably not. Most compilers are good at finding shortcuts, and might recognize that you set a to 1, and never change it, so it may not even make it a memory location and replace every appearance by the constant value 1. If you mark a as volatile, then the compiler is not allowed to take those kinds of shortcuts.

    Quote Originally Posted by GokhanK View Post
    And where can I find such a code? I want to give tries on it.
    If you had such a device, then you'd have the code (or would have easy access to it).

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Describing more indeapth what it is: The C Book — Const and volatile

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    The last question in this thread:

    There is an example which const and volatile are used together:

    Code:
    const volatile char *port = (const volatile char *) 0x30;
    What can be change and what can not in this example?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    port is a pointer to a const volatile char. 0x30 is presumably some port number or something similar -- or at least it's the address where this data lives. It's not writable by you, you can only read from that address. volatile means every time you go back, it might be different (meaning the compiler can't try to optimize).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  2. strange grammar about volatile and operator overload
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2008, 05:38 AM
  3. MSDN const_cast sample
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 12-17-2007, 08:32 AM
  4. Volatile in dos application
    By vaibhav in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2006, 11:00 PM
  5. volatile??
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 05-29-2003, 03:46 PM