0
0
Embedded Cprogramming~20 mins

Volatile variables in ISR context in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Volatile ISR Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of volatile variable update in ISR

Consider the following embedded C code snippet where a variable is updated inside an Interrupt Service Routine (ISR). What will be the output printed by the main function?

Embedded C
#include <stdio.h>
#include <stdbool.h>

volatile int flag = 0;

void ISR() {
    flag = 1;
}

int main() {
    while (!flag) {
        // waiting for ISR to set flag
    }
    printf("Flag is set to %d\n", flag);
    return 0;
}
AFlag is set to 1
BFlag is set to 0
CProgram hangs (infinite loop)
DCompilation error due to volatile usage
Attempts:
2 left
💡 Hint

Think about how volatile tells the compiler about variable changes outside normal flow.

🧠 Conceptual
intermediate
1:30remaining
Why use volatile in ISR context?

Why is it important to declare variables shared between main code and an ISR as volatile in embedded C?

ATo prevent the compiler from optimizing out reads/writes to the variable
BTo make the variable thread-safe automatically
CTo allocate the variable in a special memory section
DTo increase the variable's size to 64 bits
Attempts:
2 left
💡 Hint

Think about what happens if the compiler assumes the variable never changes unexpectedly.

🔧 Debug
advanced
2:00remaining
Identify the bug with volatile and ISR

Examine the code below. The variable count is incremented in the ISR and read in main. The program sometimes hangs in the loop. What is the bug?

Embedded C
#include <stdio.h>

int count = 0;

void ISR() {
    count++;
}

int main() {
    while (count == 0) {
        // wait for ISR to increment count
    }
    printf("Count is %d\n", count);
    return 0;
}
AMissing return statement in ISR
BVariable 'count' should be declared volatile
CThe loop condition should be 'while(count != 0)'
DISR should not modify global variables
Attempts:
2 left
💡 Hint

Think about compiler optimizations and how the main loop sees the variable.

📝 Syntax
advanced
1:30remaining
Correct volatile declaration for ISR-shared variable

Which of the following declarations correctly declares a variable status shared between main code and an ISR to ensure proper behavior?

Avolatile int status = 0;
Bint volatile status = 0;
CA, B, and D
Dstatic volatile int status = 0;
Attempts:
2 left
💡 Hint

Consider C syntax rules for volatile and static keywords.

🚀 Application
expert
2:30remaining
Effect of missing volatile on ISR flag check

Given the code below, what is the most likely behavior when the flag variable is not declared volatile?

#include <stdio.h>

int flag = 0;

void ISR() {
    flag = 1;
}

int main() {
    while (!flag) {
        // wait for ISR
    }
    printf("Flag set\n");
    return 0;
}
AThe program will crash with a segmentation fault
BThe program will print 'Flag set' immediately
CThe program will print 'Flag set' twice
DThe program may hang indefinitely because the compiler optimizes the loop assuming 'flag' does not change
Attempts:
2 left
💡 Hint

Think about compiler optimizations and how they affect variables changed outside normal flow.