Bird
Raised Fist0
Cprogramming~20 mins

calloc function - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the calloc function do in C?
easy
A. Copies memory from one location to another
B. Allocates memory but leaves it uninitialized
C. Frees previously allocated memory
D. Allocates memory for multiple elements and initializes all bytes to zero

Solution

  1. Step 1: Understand calloc purpose

    calloc allocates memory for an array of elements and initializes all bytes to zero.
  2. Step 2: Compare with other functions

    malloc allocates memory but does not initialize it, free releases memory, and memcpy copies memory.
  3. Final Answer:

    Allocates memory for multiple elements and initializes all bytes to zero -> Option D
  4. Quick Check:

    calloc = zeroed memory allocation [OK]
Hint: Remember: calloc = allocate + clear (zero) memory [OK]
Common Mistakes:
  • Confusing calloc with malloc (uninitialized memory)
  • Thinking calloc frees memory
  • Assuming calloc copies data
2. Which of the following is the correct syntax to allocate memory for 10 integers using calloc?
easy
A. int *arr = calloc(10);
B. int *arr = calloc(sizeof(int), 10);
C. int *arr = calloc(10, sizeof(int));
D. int *arr = calloc(10 * sizeof(int));

Solution

  1. Step 1: Recall calloc parameters

    calloc takes two arguments: number of elements and size of each element.
  2. Step 2: Match correct order and types

    int *arr = calloc(10, sizeof(int)); correctly uses calloc(10, sizeof(int)). Other options have wrong order or missing arguments.
  3. Final Answer:

    int *arr = calloc(10, sizeof(int)); -> Option C
  4. Quick Check:

    calloc(count, size) = correct syntax [OK]
Hint: First argument = count, second = size in calloc [OK]
Common Mistakes:
  • Swapping the two arguments
  • Passing only one argument
  • Multiplying inside calloc instead of separate arguments
3. What will be the output of this code snippet?
int *ptr = calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
    printf("%d ", ptr[i]);
}
free(ptr);
medium
A. Random garbage values
B. 0 0 0 0 0
C. Compilation error
D. Segmentation fault

Solution

  1. Step 1: Understand calloc initialization

    calloc sets all allocated bytes to zero, so all integers start as 0.
  2. Step 2: Analyze the loop output

    The loop prints each element, all zero, so output is "0 0 0 0 0 ".
  3. Final Answer:

    0 0 0 0 0 -> Option B
  4. Quick Check:

    calloc zeroes memory = zeros printed [OK]
Hint: calloc memory is always zeroed, so prints zeros [OK]
Common Mistakes:
  • Expecting garbage values like malloc
  • Forgetting to free memory (not causing output error here)
  • Assuming uninitialized values
4. Identify the error in this code:
int *arr = calloc(5, sizeof(int));
arr[5] = 10;
free(arr);
medium
A. Accessing out-of-bounds index
B. Missing free call
C. Incorrect calloc syntax
D. No error, code is correct

Solution

  1. Step 1: Check array indexing

    Array allocated has indices 0 to 4. Accessing arr[5] is out-of-bounds.
  2. Step 2: Confirm other parts

    calloc syntax is correct and free is called properly.
  3. Final Answer:

    Accessing out-of-bounds index -> Option A
  4. Quick Check:

    Index 5 invalid for size 5 array [OK]
Hint: Array indices go from 0 to size-1 [OK]
Common Mistakes:
  • Using index equal to size (off-by-one error)
  • Forgetting to free memory
  • Misunderstanding calloc parameters
5. You want to allocate memory for a 2D array of 3 rows and 4 columns using calloc. Which code snippet correctly does this?
hard
A. int (*arr)[4] = calloc(3, sizeof(*arr));
B. int **arr = calloc(3, sizeof(int*)); arr[0] = calloc(4, sizeof(int));
C. int *arr = calloc(3 * 4, sizeof(int));
D. int arr[3][4] = calloc(3, 4);

Solution

  1. Step 1: Understand 2D array allocation

    int (*arr)[4] = calloc(3, sizeof(*arr)); allocates 3 rows of arrays each with 4 ints, using pointer to array syntax.
  2. Step 2: Check other options

    int **arr = calloc(3, sizeof(int*)); arr[0] = calloc(4, sizeof(int)); only allocates first row's columns, not all rows. int *arr = calloc(3 * 4, sizeof(int)); allocates flat 1D array, not 2D. int arr[3][4] = calloc(3, 4); is invalid syntax.
  3. Final Answer:

    int (*arr)[4] = calloc(3, sizeof(*arr)); -> Option A
  4. Quick Check:

    Pointer to array + calloc rows = correct 2D allocation [OK]
Hint: Use pointer to array type for 2D calloc allocation [OK]
Common Mistakes:
  • Allocating only one row's columns
  • Using invalid syntax for arrays
  • Allocating flat 1D array instead of 2D
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard