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

C Looping Through Arrays

A for loop with an index from 0 to the array's length minus one is the standard way to visit every element of an array, whether printing, summing, searching, or modifying values.
Syntax
c
for (int i = 0; i < length; i++) {
  array_name[i];
}

Looping Through an Array with for

The standard way to loop through an array in C is a for loop whose index variable runs from 0 up to, but not including, the array's length, accessing one element per iteration with square-bracket indexing.

Example: Looping Through an Array with for

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[4] = {5, 10, 15, 20};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Print formatted text to the screen
		printf("%d ", arr[i]);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing All Elements

Looping through an array to print its contents visits each element in order, using the loop index both to access the array element and, often, to label or number the output.

Example: Printing All Elements

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("Item %d: %d\n", i, arr[i]);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Summing Array Elements

A running total variable, initialized before the loop and updated inside it, is the standard pattern for summing every element in an array, which can then be divided by the count to compute an average.

Example: Summing Array Elements

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

Finding a Value in an Array

Searching an array for a specific value loops through its elements checking each one against the target, and break is commonly used to stop the loop immediately once a match is found rather than continuing needlessly.

Example: Finding a Value in an Array

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[4] = {5, 10, 15, 20};
	// Declare target and set it to 15
	int target = 15;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Check whether arr[i] == target
		if (arr[i] == target) {
			// Print formatted text to the screen
			printf("Found at index %d", i);
			// Leave the switch/loop
			break;
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Elements in a Loop

A loop can modify an array's elements in place by both reading and writing to the same index within its body, which is how operations like doubling every value or clamping negative values are applied across an entire array.

Example: Modifying Elements in a Loop

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		arr[i] *= 2;
	}
	// Print formatted text to the screen
	printf("%d %d %d", arr[0], arr[1], arr[2]);
	// 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. Looping with i <= size, which reads one element past the end.
  2. Using sizeof(arr) inside a function to get the number of elements.
  3. Modifying elements of the wrong array or index by mixing up loop variables.
🔒

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.