This C program demonstrates iteration over various data structures, similar to the original example. Here’s an explanation of the code:
We start by summing numbers in an array using a standard for loop. In C, we need to manually calculate the length of the array.
We then demonstrate using the index in a loop to find a specific element.
To represent key-value pairs (which are natively supported in some languages), we use a simple structure in C.
We iterate over these key-value pairs, printing both keys and values, and then just the keys.
Finally, we iterate over a string, which in C is represented as an array of characters. We print both the index and the ASCII value of each character.
To compile and run this program:
This example showcases how to iterate over different types of data in C, which is conceptually similar to using range in some other languages, although the syntax and exact mechanisms differ.