Challenge - 5 Problems
Register Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of code using register variable
What is the output of this C code snippet that uses a register variable inside a loop?
C
#include <stdio.h> int main() { register int i; int sum = 0; for(i = 1; i <= 5; i++) { sum += i; } printf("%d", sum); return 0; }
Attempts:
2 left
💡 Hint
Remember that register variables are stored in CPU registers for faster access but behave like normal variables in this context.
✗ Incorrect
The register keyword suggests storing the variable in a CPU register for speed, but it does not change the logic. The loop sums numbers 1 to 5, so output is 15.
❓ Predict Output
intermediate2:00remaining
Effect of register keyword on address operator
What happens when you try to print the address of a register variable in C?
C
#include <stdio.h> int main() { register int x = 10; printf("%p", (void*)&x); return 0; }
Attempts:
2 left
💡 Hint
The register keyword restricts taking the address of the variable.
✗ Incorrect
Register variables are stored in CPU registers and their address cannot be taken, so the compiler throws an error when & operator is used.
🔧 Debug
advanced2:00remaining
Identify the error with register variable usage
What error will this code produce and why?
C
#include <stdio.h> int main() { register int count = 5; int *ptr = &count; printf("%d", *ptr); return 0; }
Attempts:
2 left
💡 Hint
Check if the address of a register variable can be taken.
✗ Incorrect
Taking the address of a register variable is not allowed, so the compiler will produce an error at the line int *ptr = &count;
🧠 Conceptual
advanced2:00remaining
Behavior of register variables in recursive functions
Consider a recursive function using a register variable as a counter. What is the behavior of the register variable across recursive calls?
C
int factorial(int n) { register int count = 0; count++; if (n <= 1) return 1; return n * factorial(n - 1); }
Attempts:
2 left
💡 Hint
Think about variable scope and lifetime in recursive calls.
✗ Incorrect
Each recursive call creates a new instance of the register variable count initialized to 0, so count does not accumulate across calls.
❓ Predict Output
expert2:00remaining
Number of items in a register variable array
What is the output of this code that declares a register array and prints its size?
C
#include <stdio.h> int main() { register int arr[4] = {1, 2, 3, 4}; printf("%lu", sizeof(arr)/sizeof(arr[0])); return 0; }
Attempts:
2 left
💡 Hint
Check if arrays can be declared with register storage class.
✗ Incorrect
In C, register storage class cannot be applied to arrays, so this code causes a compilation error.