← Back to C Course | Chapter 6: Arrays & Strings | Lesson 2 of 8

C Array Size

An array's size is fixed at declaration, and sizeof combined with dividing by one element's size is the standard way to compute how many elements it holds, especially before passing it to a function.
Syntax
c
data_type array_name[size] = {value1, value2};

int length = sizeof(array_name) / sizeof(array_name[0]);

Fixed Size at Declaration

An array's size is fixed at the moment it's declared, specified as a number inside square brackets, and unlike some other languages, a standard C array cannot grow or shrink after that.

Example: Fixed Size at Declaration

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[5];
	// Print formatted text to the screen
	printf("%zu", sizeof(arr));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding an Array's Size with sizeof

The sizeof operator applied to an array returns the total number of bytes it occupies, and dividing that by the size of a single element, using sizeof(arr[0]), is the standard way to compute how many elements it holds.

Example: Finding an Array's Size with sizeof

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[5] = {1, 2, 3, 4, 5};
	// Declare length and set it to sizeof(arr) / sizeof(arr[0])
	int length = sizeof(arr) / sizeof(arr[0]);
	// Print formatted text to the screen
	printf("%d", length);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Size vs Number of Elements

An array's total byte size and its element count are related but different: total bytes equals the element count multiplied by the size of each individual element, which matters for types larger than one byte.

Example: Array Size vs Number of Elements

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	double arr[4];
	// Print formatted text to the screen
	printf("Bytes: %zu, Elements: %zu", sizeof(arr), sizeof(arr) / sizeof(arr[0]));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Size of Arrays Passed to Functions

When an array is passed as a function parameter, it decays into a pointer, so sizeof inside that function reports the size of the pointer rather than the original array, requiring the element count to be passed in separately.

Example: Size of Arrays Passed to Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void showSize(int arr[]) {
	// Print formatted text to the screen
	printf("%zu", sizeof(arr));
}
// Program execution starts in main()
int main() {
	int numbers[5] = {1, 2, 3, 4, 5};
	showSize(numbers);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Sizing Arrays for Input

Arrays intended to hold user input or other variable amounts of data are still given a fixed maximum capacity, often defined with a named constant, and the program tracks separately how many of those slots are actually in use.

Example: Sizing Arrays for Input

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_SIZE (replaced by the preprocessor)
#define MAX_SIZE 10
// Program execution starts in main()
int main() {
	int data[MAX_SIZE];
	// Print formatted text to the screen
	printf("%d", MAX_SIZE);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using sizeof(arr) inside a function where the array has decayed to a pointer, giving 8 instead of the length.
  2. Forgetting to divide sizeof(arr) by sizeof(arr[0]) to get the element count.
  3. Using a non-constant size in old C89 code, which is not allowed.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.