`volatile` — C Keyword
`volatile` — C Keyword
The volatile keyword in C: prevents the compiler from optimizing away accesses.
`volatile` — C Keyword
The volatile keyword in C: prevents the compiler from optimizing away accesses.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
volatile (C)Marks a variable as potentially modified by external means (hardware registers, signal handlers). The compiler must not cache or eliminate reads/writes to volatile objects.
volatile Type name;
volatile Type* ptr;
#include <signal.h>
#include <stdio.h>
/* Signal handler pattern */
volatile sig_atomic_t g_stop = 0;
void handle_sigint(int sig) {
(void)sig;
g_stop = 1;
}
/* Memory-mapped register example */
volatile unsigned int* const STATUS_REG =
(volatile unsigned int*)0x40000000u;
void wait_ready(void) {
while (!(*STATUS_REG & 1u)) {
/* busy-wait – volatile prevents the compiler hoisting the read */
}
}
int main(void) {
signal(SIGINT, handle_sigint);
while (!g_stop) {
/* main loop */
}
printf("stopped\n");
return 0;
}
volatile does not provide atomicity or memory ordering; use _Atomic for thread-safe access.int main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}