calloc Allocation in C

Level 4142 keystrokes

Uses calloc to allocate zero-initialized memory for an array.

C code

// calloc(count, size) allocates and zeros memory
1int *arr = (int *)calloc(5, sizeof(int));
// Always check if allocation succeeded
2if (arr != NULL) {
// All elements are initialized to 0
3 for (int i = 0; i < 5; i++) {
// Prints 0 0 0 0 0 without any explicit assignment
4 printf("%d ", arr[i]);
5 }
6 printf("\n");
// Free allocated memory
7 free(arr);
8}

Output

0 0 0 0 0
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • (× 8
  • )× 8
  • ;× 6
  • "× 4
  • =× 3
  • ,× 2
  • {× 2
  • }× 2
  • *× 2
  • +× 2

More C examples at level 4

← C typing practice