0
0
Cprogramming~20 mins

Register storage class - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Register Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AUndefined behavior
B10
C15
DCompilation error due to register variable
Attempts:
2 left
💡 Hint
Remember that register variables are stored in CPU registers for faster access but behave like normal variables in this context.
Predict Output
intermediate
2: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;
}
ACompilation error: cannot take address of register variable
BPrints the memory address of x
CRuntime error
DPrints 0
Attempts:
2 left
💡 Hint
The register keyword restricts taking the address of the variable.
🔧 Debug
advanced
2: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;
}
APrints 5
BCompilation error: cannot take address of register variable
CRuntime segmentation fault
DUndefined behavior but compiles
Attempts:
2 left
💡 Hint
Check if the address of a register variable can be taken.
🧠 Conceptual
advanced
2: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);
}
AThe count variable is shared globally among all calls
BThe register variable count keeps incrementing across all recursive calls
CCompilation error due to register variable in recursion
DEach recursive call has its own separate register variable count initialized to 0
Attempts:
2 left
💡 Hint
Think about variable scope and lifetime in recursive calls.
Predict Output
expert
2: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;
}
ACompilation error: register variable cannot be an array
B4
C1
DUndefined behavior
Attempts:
2 left
💡 Hint
Check if arrays can be declared with register storage class.