For Each Loop in C

Muhammad Zeeshan Oct 12, 2023
  1. Loops in Programming Languages
  2. the for-each Loop in C
  3. Use Macros to Implement the for-each Loop in C
For Each Loop in C

The following content will investigate whether or not C language supports a for-each loop. First of all, we must precisely know what loops are.

Loops in Programming Languages

The execution of a statement or collection of words in a programming language may be looped to perform many times, with the number of repetitions being determined by the conclusion of an evaluation of a condition. The consequent condition must be satisfied for statements to be executed inside loops.

Iterating through the collection components may be accomplished with the help of the for-each loop. The collection may be a list or an array.

It carries out its operations for each element of the array.

the for-each Loop in C

The for-each construct is not supported in C and cannot be implemented. When an array is parsed using the point notation, the receiver does not know how long the array is.

Hence, there is no way to determine when you have reached the end of the array. Remember that an int* variable in C is a pointer to a location in memory that contains an int.

No header object has information on the number of numbers arranged in sequence, and this is because there is none. As a result, the programmer must keep track of this.

On the other hand, creating something that functions analogously to a for-each loop when working with lists is simple.

for (Node* node = head; node; node = node.next) {
  // Your logic will be here
}

You have the option of doing any of these two things to accomplish the same thing using arrays.

  1. The array’s length should be stored in the first member of the array.
  2. Encapsulate the array inside a struct that stores its length and a reference to the array itself.

One such structure is shown in the following example.

typedef struct job_t {
  int countvariable;
  int* arr;
} arr_t;

Use Macros to Implement the for-each Loop in C

In addition, we may utilize macros to simplify the code and make it simpler to understand and write. For some data structures, we can build macros to implement the for-each construct in C.

To acquire a better understanding of this concept, let’s take a look at the following example of it.

#include <stdio.h>

int main() {
#define FOREACH(item, arr, start, size)                              \
  for (int i = start, keep = 1; keep && i < size; keep = !keep, i++) \
    for (item = arr[i]; keep; keep = !keep)

  int arr[] = {3, 9, 7, 1, 8};
  FOREACH(int z, arr, 3, 7)
  printf("Shanii Demo index: %d. element: %d\n", i, z);
}

You can now define the start index and the size so that it works on decaying arrays pointers. There is no need for int* and count!= size has been changed to i<size in case the user inadvertently alters i to be larger than size, which would cause them to be caught in an endless loop.

Output:

Shanii Demo index: 3. element: 1
Shanii Demo index: 4. element: 8
Shanii Demo index: 5. element: 32766
Shanii Demo index: 6. element: -1762484992
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn