Last active
March 15, 2018 19:35
-
-
Save zbanks/2a87213dbf1a814161be8a1585302d9a to your computer and use it in GitHub Desktop.
Backwards for loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/gcc -Wall -Wextra -Wpedantic -Wconversion -std=c99 reverse_for.c -o reverse_for && ./reverse_for | |
// https://github.com/zbanks | |
#include <stdio.h> | |
#define for(BODY) for2(__COUNTER__, _count, _i, BODY) | |
#define for2(_uniq, _count, _i, BODY) for3(_uniq, _count, _i, BODY) | |
#define for3(_uniq, _count, _i, BODY) for4(_count ## _uniq, _i ## _uniq, BODY) | |
#define for4(_count, _i, BODY) \ | |
long _count = 1; \ | |
for (BODY) _count++; \ | |
for (long _i = 1; _count; _count--, _i = 1) \ | |
for (BODY) \ | |
if (_i++ == _count) | |
int main(void) { | |
// Prints "9 8 7 6 5 4 3 2 1 0" | |
for (int i = 0; i < 10; i++) { | |
printf("%d ", i); | |
} | |
printf("\n"); | |
// Prints "!dlrow ,olleH" | |
const char * string = "Hello, world!"; | |
for (const char * ptr = string; *ptr; ptr++) { | |
printf("%c", *ptr); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment