0
0
Embedded Cprogramming~20 mins

Volatile keyword and why it matters in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Volatile Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using volatile?
Consider the following embedded C code snippet. What will be the output printed by the program?
Embedded C
#include <stdio.h>

volatile int flag = 0;

void setFlag() {
    flag = 1;
}

int main() {
    while(flag == 0) {
        // waiting for flag to change
    }
    printf("Flag changed!\n");
    return 0;
}

// Assume setFlag() is called asynchronously by an interrupt or another thread.
AThe program crashes with a segmentation fault.
BThe program runs forever and never prints anything.
CThe program prints "Flag changed!" multiple times.
DThe program prints "Flag changed!" and exits.
Attempts:
2 left
💡 Hint
Think about how the volatile keyword affects compiler optimizations and variable visibility.
Predict Output
intermediate
2:00remaining
What happens if volatile is removed?
Given the same code as before but without the volatile keyword on 'flag', what is the most likely behavior?
Embedded C
#include <stdio.h>

int flag = 0;

void setFlag() {
    flag = 1;
}

int main() {
    while(flag == 0) {
        // waiting for flag to change
    }
    printf("Flag changed!\n");
    return 0;
}

// Assume setFlag() is called asynchronously by an interrupt or another thread.
AThe program prints "Flag changed!" and exits.
BThe program prints "Flag changed!" multiple times.
CThe program runs forever and never prints anything.
DThe program crashes with a segmentation fault.
Attempts:
2 left
💡 Hint
Consider how the compiler might optimize the loop without volatile.
🔧 Debug
advanced
2:00remaining
Identify the problem with this volatile usage
What is the main issue with the following code snippet regarding volatile usage?
Embedded C
volatile int *ptr = (volatile int *)0x40000000;

int main() {
    *ptr = 5;
    int val = *ptr;
    if(val == 5) {
        // do something
    }
    return 0;
}
AThe data pointed to is volatile but the pointer is not volatile, which is correct.
BThe pointer and the data it points to should both be volatile.
CThe pointer is volatile but the data it points to is not volatile.
DThe pointer itself should be volatile, not the data it points to.
Attempts:
2 left
💡 Hint
Think about what volatile means for pointers and the data they point to.
📝 Syntax
advanced
2:00remaining
Which declaration correctly uses volatile for a hardware register?
Choose the correct declaration for a volatile pointer to a 32-bit hardware register at address 0x40001000.
Avolatile int *reg = (volatile int *)0x40001000;
Bint volatile *reg = (int volatile *)0x40001000;
Cint * volatile reg = (int *)0x40001000;
Dvolatile int volatile *reg = (volatile int volatile *)0x40001000;
Attempts:
2 left
💡 Hint
Remember that 'volatile' can appear before or after the type and applies to the pointed data.
🚀 Application
expert
3:00remaining
Why is volatile critical in embedded interrupt handling?
In embedded systems, why is it important to declare variables shared between main code and interrupt service routines (ISRs) as volatile?
ABecause volatile prevents the compiler from optimizing away reads and writes, ensuring the main code sees changes made by the ISR.
BBecause volatile makes the variable thread-safe and automatically locks it during ISR execution.
CBecause volatile increases the speed of variable access in ISRs.
DBecause volatile allows the variable to be stored in faster CPU registers.
Attempts:
2 left
💡 Hint
Think about how the compiler treats variables that can change unexpectedly.