Home > Tutorials > C Tutorials > Programming In C: Arrays and Loops

Programming In C: Arrays and Loops

Arrays and Loops

If you need to perform a function on each element in an array, then use a for loop. Set the counter at 0, so that the loop will start with the array element in index 0, and then loop through each element of the array:

for(i = 0; i < number of elements; i++)

In this case, the number of elements would be the size of the array.

For example, if we have an array with 4 elements, then the loop would be: for(i = 0; i < 4; i++).

To loop through each element of the array, we need to use the variable i as an index number in our print statment, like this:

printf(“%d”, index[i]);

Putting the loop and the printf statements together, we can print each element of the array:

for(i = 0; i < number of elements; i++)

printf(“%d”, index[i]);

Now let’s use this with a real array:

int numbers[4] = { 22, 33, 44, 55 };
for(i = 0; i < 4; i++) 

printf(“%d”, numbers[i]);


This would print:


Sample Array Loop

Let’s look at an example that loops through each element of an array. During the 20th century, geologists began drilling into the glaciers of Antarctica and Greenland, to extract long ice cores. These cores contain records of Earth’s climate history over the past millenia. Geologists can determine when ice ages, volcanic eruptions, and other climate changes occurred by studying these ice cores.

The northernmost ice core samples come from the GRIP (Greenland Ice Project). These samples reveal huge amounts of acid snow and dust during years of massive volcanic eruptions. Here is a script that tells about some of these eruptions:

void main( )
int i = 0;
int eruption[4] = { 536, 626, 934, 1258 };
printf(“\nThe GRIP ice cores suggest massive volcanic eruptions during these years:”);
for(i = 0; i < 4; i++)
printf(“\n%d AD”, eruption[i]);


Save your source code as erupt.c. Compile it. Link it. Run it.

This program loops through each element of the eruption array, and prints it with AD. Your output should be:

The GRIP ice cores suggest massive volcanic eruptions during these years:
536 AD
626 AD
934 AD
1258 AD


About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Setup Linux on Amazon EC2 with Apache, PHP & MySQL

AWS is quickly turning into an extremely common solution – offering significantly more features than ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.