Hello Everyone,
I am getting confused when I read about the volatile keyword in a c language. The definition says that the volatile keyword informs the compiler that the value of a variable may change at any time from outside. It also instructs the compiler to read the value of the variable each time before using it. I see this is commonly used for variables that are accessed in an Interrupt Service Routine (ISR) or when reading pin status.
My question is, in a C program, there can be multiple variables, so why does the compiler optimize specific variables and not others? When the program doesn't read the value from the original address, what does it read and where does it read it from?
I am getting confused when I read about the volatile keyword in a c language. The definition says that the volatile keyword informs the compiler that the value of a variable may change at any time from outside. It also instructs the compiler to read the value of the variable each time before using it. I see this is commonly used for variables that are accessed in an Interrupt Service Routine (ISR) or when reading pin status.
My question is, in a C program, there can be multiple variables, so why does the compiler optimize specific variables and not others? When the program doesn't read the value from the original address, what does it read and where does it read it from?