Sizeof Operator in C

Level 3108 keystrokes

Uses sizeof operator to calculate the number of elements in an array.

C code

// The length 5 is not written anywhere; it comes from the initializer
1int arr[] = {1, 2, 3, 4, 5};
// Total bytes ÷ bytes per element = element count (e.g. 20 / 4 = 5)
// This works only on a real array, not on a pointer or array parameter
2int size = sizeof(arr) / sizeof(arr[0]);
// Print Array has 5 elements
3printf("Array has %d elements", size);

Output

Array has 5 elements
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.

  • ,× 5
  • ;× 3
  • (× 3
  • )× 3
  • "× 2
  • [× 2
  • ]× 2
  • =× 2
  • {× 1
  • }× 1

More C examples at level 3

← C typing practice