Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 930201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:17:48+00:00 2026-05-15T20:17:48+00:00

I know that by the volatile keyword, volatile int k=7; we hint the compiler

  • 0

I know that by the volatile keyword,

volatile int k=7; 

we hint the compiler that the variable can be changed at any time but what about a simple
int k=7? Can we change it at any time because it is not constant? What is different?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:17:49+00:00Added an answer on May 15, 2026 at 8:17 pm

    It’s used in low level programming with interrupts and so on mostly

    volatile int count;
    
    void test()
    {
       while(count< 100) 
       {
            // do nothing
       }
    }
    
    //
    // Interrupt function called by the hardware automatically at intervals
    //
    void interrupt()
    {
        count = count + 1;
    }
    

    if you don’t declare the variable as volatile the compiler will probably notice that count can’t change in the while loop and so won’t bother to actually read the value every time from memory so it will never exit the loop.

    If you declare it volatile then the compiler will read the value from memory every time as you’ve told it that the value might change without notice…

    Another use is to map hardware ports.

    On a microcontroller you might have some digital inputs which “appear” at a certain memory address. You can read them as if they were a variable but of course the value will potentially change all the time depending on the input signals. Declaring the value as volatile will indicate to the compiler that yes you actually do need to read this memory location every single time because it might have changed, and no you can’t assume that it won’t change unless you change it.

    Unless you are using low level interrupts or some uses of threading then you don’t need to use it.

    EDIT: To be clear, volatile is NOT for synchronization between theads in standard c++, it only does a part of what is necessary. The last sentence in my original post could be misleading. The examples and stuff about hardware interrupts etc in my post are what volatile is for, it absolutely ISN’T for threading and don’t even try to use it for that.

    I originally wrote “some uses of threading” because on some platforms it might be sufficient to use volatile. This is BAD ADVICE in general but if you have a single core and all writes are visible to all “threads” then it might work for you. For example on a microcontroller with a simple interrupt based thread switching system it would likely “work” even though not guarenteed by the standard. But don’t do it. In general it’s just wrong, and c++11 has ways that actually work (remember this answer was written pre c++11)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that the following is true int i = 17; //binary 10001 int
I know that IList is the interface and List is the concrete type but
We know that IPhone has a push notification function. But I have different kinds
I know that question was discussed plenty of times, but i tried all suggestions
I know that default cron's behavior is to send normal and error output to
I know that .NET is JIT compiled to the architecture you are running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.