GeeksforGeeks » C/C++ Programming Questions

volatile and constant variable

(4 posts)

Tags:

  1. Venki
    Moderator
    Posted 1 year ago #

    Can a variable be qualified with both const and volatile at the same time?

    extern int const volatile *port;

    Is the above code allowed in C/C++ programming?

  2. kartik
    Moderator
    Posted 1 year ago #

    the concepts of const and volatile are completely independent. A variable can be both const and volatile. See http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

  3. Venki
    Moderator
    Posted 1 year ago #

    @Karthik, thanks for the link.

    I posted this question to clarify the understanding of volatile and const in C/C++ (particularly beginners).

    As a rule of thumb, use const as a promise made by compiler and volatile as, commanding compiler to exclude particular identifier from optimization. When an identifier is made as both const and volatile, it signifies that the variable can be modified by an external agent (hence no optimizations) and it should not be modified within the scope of current program.

    There are many implications of const and volatile qualified names. It requires time and space to cover, let's hope Gautham will post them in G-Facts.

  4. sreeja
    guest
    Posted 4 months ago #

    m a 4th semester engg student n this is a C lab viva question which was given 2 us. but i dont know what volatile means?


Reply

You must log in to post.

RSS feed for this topic