Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the purpose of the 'volatile' keyword in programming languages like C/C++ ?

  • A.Prevents variable optimization by the compiler
  • B.Makes variable constant
  • C.Allocates variable on heap
  • D.Enables multi-threading

Answer: A

The volatile keyword prevents variable optimization by the compiler.

Declaring a variable volatile tells the compiler its value can change outside the program's visible flow, for example through a memory-mapped hardware register, an interrupt handler or a signal. The compiler must then read and write the variable in memory every time, instead of keeping it in a register or removing 'redundant' accesses.

It does not make a variable constant (that is const), and it does not decide where the variable is stored. It does not enable multi-threading, and in C and C++ it is not a substitute for proper thread synchronisation.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.