ProgrammingModerateProduct company OA
When should a variable be declared volatile?
- A. When it should be kept in a CPU register for speed
- B. When it must never be modified after initialisation
- C. When it is shared between several source files
- D. When its value can change in ways the compiler cannot see, such as a hardware register or a flag set by a signal handler
Correct answer: D. When its value can change in ways the compiler cannot see, such as a hardware register or a flag set by a signal handler
volatile tells the compiler that every read and write must really happen, because something outside the code it can see may change or observe the value. Sharing between files is extern, the register hint is register, and read-only is const.
More C Programming questions
- What is printed? c void f(int a, int n) { if (n <= 1) return; a[n - 2] += a[n - 1]; f(a, n - 1); } int main(void) { int a[] = {1, 2, 3…
- With a 32-bit unsigned int x, which expression has undefined behaviour?
- What is printed? c char s[] = "7392"; int n = 0; for (int i = 0; s[i]; i++) n = n 10 + (s[i] - '0'); printf("%d %d", n % 1000, n / 100 %…
- What is printed? c int f(int n) { if (n == 0) return 0; return (n & 1) + f(n >> 1) 10; } int main(void) { printf("%d", f(13)); }
- What is printed? c struct S { int a; int b; } s[] = {{1, 10}, {2, 20}, {3, 30}}; struct S p = s; int x = ++p->a; int y = (++p)->b; int z =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x12345678; printf("%X", (x >> 16) | (x << 16));