0

What would happen when we use the volatile in local variable?

For example,

    #include <stdio.h>
    #include <stdlib.h>

    int get_data (volatile int data)
    {
        return (data * data * data);
    }

    int main()
    {
        int data = get_data(12);
        printf("%d",data);
        return 0;
    }
selva
  • 97
  • 1
  • 5

2 Answers2

2

In this case, there would be no visible change, other than the program possibly executing slower.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

When a variable is qualified by volatile in its declaration, then the value of variable can be changed by any external device or hardware interrupt. In your example, there is no change. When you qualify a variable volatile, whenever the program tries to access the variable, every time it will look into to the memory where the variable is stored. There will not be any optimization done for the variable. So the program might run slower.

 What is best use volatile?

https://stackoverflow.com/a/4437555/1814023

Community
  • 1
  • 1