0
0
Cprogramming~20 mins

calloc function - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of calloc
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 calloc usage?
Consider the following C code snippet using calloc. What will be printed?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = (int *)calloc(5, sizeof(int));
    if (!arr) return 1;
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}
A0 0 0 0 0
BRandom garbage values printed
CCompilation error due to missing header
DSegmentation fault at runtime
Attempts:
2 left
💡 Hint
calloc initializes allocated memory to zero.
Predict Output
intermediate
2:00remaining
What happens if calloc is called with zero elements?
What will be the output or behavior of this code?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)calloc(0, sizeof(int));
    if (ptr == NULL) {
        printf("NULL pointer\n");
    } else {
        printf("Non-NULL pointer\n");
    }
    free(ptr);
    return 0;
}
ANULL pointer
BRuntime crash
CCompilation error
DNon-NULL pointer
Attempts:
2 left
💡 Hint
calloc with zero elements may return a unique pointer that should not be dereferenced.
🔧 Debug
advanced
2:00remaining
Why does this calloc usage cause a runtime error?
Examine the code below. It compiles but crashes at runtime. What is the cause?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = calloc(5, sizeof(int));
    for (int i = 0; i <= 5; i++) {
        arr[i] = i * 2;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}
Acalloc was not cast to int* causing type mismatch
BThe printf format specifier is incorrect
CThe loop writes out of bounds at index 5 causing undefined behavior
DMemory was not freed before program ends
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between malloc and calloc?
Choose the correct statement about calloc compared to malloc.
Acalloc allocates memory faster than malloc always
Bcalloc allocates and initializes memory to zero, malloc only allocates without initialization
Cmalloc allocates memory and initializes it to zero, calloc does not initialize
Dmalloc returns NULL on failure, calloc aborts the program
Attempts:
2 left
💡 Hint
Think about what happens to the memory content after allocation.
Predict Output
expert
2:00remaining
What is the output of this calloc and pointer arithmetic code?
Analyze the code and select the correct output.
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = calloc(3, sizeof(int));
    if (!p) return 1;
    *(p + 1) = 10;
    p++;
    printf("%d\n", *p);
    free(p - 1);
    return 0;
}
A10
B0
CUndefined behavior due to invalid free
DCompilation error due to pointer arithmetic
Attempts:
2 left
💡 Hint
Remember how pointer arithmetic and calloc initialization work.