Challenge - 5 Problems
Static Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of static variable initialization
What is the output of this C code snippet using static variables?
Embedded C
#include <stdio.h> void func() { static int count = 0; count++; printf("%d ", count); } int main() { for(int i = 0; i < 3; i++) { func(); } return 0; }
Attempts:
2 left
💡 Hint
Remember that static variables keep their value between function calls.
✗ Incorrect
The static variable 'count' is initialized once to 0 and retains its value between calls. Each call increments it by 1, so the output is '1 2 3 '.
❓ Predict Output
intermediate2:00remaining
Static array initialization and modification
What will be the output of this code that uses a static array inside a function?
Embedded C
#include <stdio.h> void updateArray() { static int arr[3] = {1, 2, 3}; arr[0] += 1; arr[1] += 1; arr[2] += 1; for(int i = 0; i < 3; i++) { printf("%d ", arr[i]); } } int main() { updateArray(); updateArray(); return 0; }
Attempts:
2 left
💡 Hint
Static arrays keep their values between function calls and are modified each time.
✗ Incorrect
The static array is initialized once. Each call increments each element by 1 and prints the updated array. So first call prints '2 3 4 ', second call prints '3 4 5 '.
🔧 Debug
advanced2:00remaining
Identify the error in static pointer usage
What error will this code produce when compiled or run?
Embedded C
#include <stdio.h> void func() { static char *ptr; char buffer[10] = "hello"; ptr = buffer; printf("%s\n", ptr); } int main() { func(); func(); return 0; }
Attempts:
2 left
💡 Hint
Think about the lifetime of local variables and static pointers.
✗ Incorrect
The pointer 'ptr' is static, but it points to 'buffer', a local array that ceases to exist after func returns. Using ptr later causes undefined behavior.
📝 Syntax
advanced1:30remaining
Static variable declaration syntax
Which option correctly declares a static variable inside a function that retains its value between calls?
Attempts:
2 left
💡 Hint
The 'static' keyword comes before the type in C declarations.
✗ Incorrect
The correct syntax is 'static int count = 0;'. Other options have incorrect order of keywords causing syntax errors.
🚀 Application
expert3:00remaining
Static memory allocation for a fixed-size buffer
You want to create a function that returns a pointer to a fixed-size buffer that retains its content between calls. Which code snippet correctly implements this using static memory allocation?
Attempts:
2 left
💡 Hint
Static arrays inside functions keep their memory allocated between calls without dynamic allocation.
✗ Incorrect
Option D declares a static array inside the function, so the buffer persists between calls and the pointer returned is valid. Option D returns pointer to local array (invalid). Option D uses dynamic allocation but does not free memory (not static allocation). Option D mixes static pointer with malloc which is not static allocation pattern.