I was getting mighty tired of typing for(int i=0; i< length; i++), and I wanted an "enhanced for loop"/"for each loop" like they have in other languages, where you can type for(x in y){} (I was willing to accept minor syntactic variation.) I decided I would try to make one in c preprocessor macro, and call it foreach. I succeeded. In lieu of making a full-blown "foreach.h" for my results, I present them here, in this blog post. Feel free to use them! This blog post is in the public domain, so you are free to use any of this code without crediting me... but, it will probably be helpful for your colleagues if you stick the url of this blog post into a comment near whatever code you copy. They may desire an explanation. I wanted to make especially sure that my macro didn't leak variable definitions into the greater namespace. Some consideration revealed that this probably wouldn't be difficult because the variables set up for a for loop (eg int i) are only accessible in the for loop, as god intended. (It's also pretty trivial to use a bare code block using curly braces {like so} to set up an appropriate lexical scope that allows the desired variable shadowing and hiding. However, it should be noted that the user will expect to be able to use flow control statements like break and continue in an enhanced for loop. This is not much of a problem, because the macro will presumably expand to something with a loop construct in it, in which those statements will work. However, if the macro expands to a for loop embeded in a code block, a break statement will only break out of the for loop, not the code block. So, one would have to be careful to put no code between the end of the loop and the end of the block, or put any necessary "clean up" code for the macro there if need be.) I got as far as #define foreach(var, array) for(unsigned int var = 0; var < sizeof(array)/sizeof(array[0]); var++) But this still would have required the user to call array[var], which I thought was inelegant. And, basically against the spirit of the enhanced for loop. but for it to be a real foreach you'd have to define var_index like we did var here, and then a typeof(array[0]) var = array[var_index]; ... but you can't declare two different type of variables in the init_clause of a for loop :,( Luckily, an example on wikipedia got me thinking about the possibility of writing #define foreach(var, array) for(typeof(array[0]) *var = array; var < array; var++) But even then you still have to use *var. I wanted var plain, dammit. And then, in a flash of inspiration, I remembered one of the basic trip-ups of C that you have to master early on: You shouldn't write int* i, j;, no matter how much you want to, because that implies i and j are both pointers to int. But, instead, only i is a pointer to int, which is why you have to write int *i, j; to reflect this state of affairs. This had never been useful before, and indeed seemed to until then to be one of the many flaws in C's type system and syntax. But now, ah! #define foreach(var, array) for(typeof(array[0]) *var##_p = array, var = *var##_p; var##_p < array + sizeof(array)/sizeof(array[0]); var = *++var##_p) Works perfectly. (you also have to know that ## pastes tokens together in preprocessor) You can now write a foreach(var, array){} loop, in which var is the data and var_p is a pointer to that record. A more ideal implementation might provide us with var and var_index, but C makes that impossible to do cleanly. But, this is still pretty good, and you can var_p-array if you need the index. Example calling code: int a[] = {69, 57, 21}; foreach(i,a){ //expands to standard for loop printf("%p %ld %d\n", i_p, i_p-a, i); } foreach(i,a){ //nested for works properly without the i values colliding (variable shadowing) printf("%p %d\n", i_p, i); foreach(i,a){ printf("%p %d\n", i_p, i); } } foreach(i,a){ //nested for works properly and you can use outer vars in inner loops printf("%p %d\n", i_p, i); foreach(j,a){ printf("%d %d\n", i, a[j]); } } A more natural example might be something like: foreach(user, users){ puts(user.name); } This simple example immediately calls to our attention the fact that we are doing a bunch of assigning/accessing behind the scenes that the compiler may or may not optimize out... ah, no matter. Of course, our foreach here is only usable for simple arrays, so I immediately wrote a version of this for strings (and other zero-terminated things) #define foreachz(var, string) for(typeof(string[0]) *var##_p = string, var = *var##_p; var!=0; var = *++var##_p) And a version for a length which is known but not available to the compiler #define foreachn(var, array, n) for(typeof(array[0]) *var##_p = array, var = *var##_p; var##_p < array + n; var = *++var##_p) I was initially able to restrain myself from writing a version for linked lists. Because, who uses linked lists? And, furthermore, it seemed to me that writing a generic version for linked lists was implausible because the pointer to the next node could be named anything, like "next" or "node". So the macro would have to use that variable name in it, either in a standardized way or by being supplied the variable name, either of which is messy. However, I realized any C programmer worth his salt would likely have the pointer be the first member of the node struct (I can't explain this hunch but I'm fairly sure that that's the best layout) like so struct linked_list { struct linked_list *next; int data; //example... could be many fields in practice }; So, theoretically, one could write a linked list version #define foreachl(var, linked_list) for(typeof(linked_list) *var##_p = &linked_list, var = *var##_p; var##_p!=0 && ( var = *var##_p , 1); var##_p = *(typeof(linked_list)**)(void*)var##_p) Note that we do some weird stuff in the condition expression so we can iterate over all non-null nodes without fencepost errors nor segfaulting. Note also that we use more indirection than conceptually necessary in the end expression to avoid C complaining about us trying to cast a struct into a pointer... it's fine, the compiler will probably optimize out our indirection anyway. (Indeed, on at least O1 the entire thing is optimized out in this toy example https://godbolt.org/z/Y7v3M199d .) (Fun fact I learned while writing the previous paragraph: according to the 1999 C standard ยง6.8.5.3, the technical names of the condition expression and the end expression are expression-2 and expression-3, respectively. The first expression is called clause-1, though, presumably because it can also be a declaration, not just an expression.) For some reason typeof isn't a standard c feature, even though it's in virtually all the compilers. One could define a three-argument standard-compliant version of the foreach macro #define foreach(type, var, array) for(type *var##_p = array, var = *var##_p; var##_p < array + sizeof(array)/sizeof(array[0]); var = *++var##_p) int a[] = {69, 57, 21}; foreach(int, i, a){ //standard for loop printf("%p %ld %d\n", i_p, i_p-a, i); } which is more like the java for(int i : a) But I like the brevity of the 2-argument version, personally. The standard-compliant version also has the weakness that if you want to pass in a type like, say, int*, you have to typedef it to, say, intypointy first, so that the declarations of var and var_p will be valid. Example full, compiling programs demonstrating the difference between the regular version and the standard-compliant version: #include #define foreach(var, array) for(typeof(array[0]) *var##_p = array, var = *var##_p; var##_p < array + sizeof(array)/sizeof(array[0]); var = *++var##_p) int main(void) { printf("Hello World\n"); int x=69, y=4, z=20; int *a[] = {&x, &y, &z}; foreach(i, a){ //standard for loop printf("%ld %d\n", i_p-a, *i); } return 0; } #include #define foreach(type, var, array) for(type *var##_p = array, var = *var##_p; var##_p < array + sizeof(array)/sizeof(array[0]); var = *++var##_p) typedef int * intypointy; int main(void) { printf("Hello World\n"); int x=69, y=4, z=20; int *a[] = {&x, &y, &z}; foreach(intypointy, i, a){ //standard for loop printf("%ld %d\n", i_p-a, *i); } return 0; } Example full, compiling program demonstrating the linked list version, also demonstrating the copyness of the local variables: #include #define foreachl(var, linked_list) for(\ typeof(linked_list) *var##_p = &linked_list, var = *var##_p;\ var##_p!=0 && ( var = *var##_p , 1); \ var##_p = *(typeof(linked_list)**)(void*)var##_p) struct linked_list { struct linked_list *next; int data; }; int main() { struct linked_list l3 = {0, 3}; struct linked_list l2 = {&l3, 2}; struct linked_list l = {&l2, 1}; foreachl(d, l){printf("%d", d.data);d.data=0;} //123 foreachl(d, l){printf("%d", d.data);d_p->data=0;} //123 foreachl(d, l){printf("%d", d.data);} //000 return 0; } Feel free to use any and all of the code in this blog post in your own projects; I've released the blog post and the code into the public domain. Note, however, that it's probably more trouble than it's worth to use some of these. Also note that, importantly, var will be a COPY of the array member, so assigning to it will not affect the original data structure. You'll have to assign through var_p.