Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ARRAY_SIZE 20
#define PNT(s, l, a) fprintf(std##s, l, a)
#define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)
void print_array(int *array) {
int x = ARRAY_SIZE;
char* fmts[] = { "%d, ", "%d\n" }; /* fmts has two strings in it */
for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;); /* These for loops are really neat!
I can do a bunch of things all in one line! Do something, test a conditional, do another thing! Or I can leave some of those
out, if I don't need them. */
for (;;) break; /* I can even do this! */
}
main() {
int iarray[ARRAY_SIZE];
int x, y;
int ofs = iarray - &ofs; /* ofs is an offset */
void (*prnt_func)/* These comment things are pretty neat too. I can add annotations to my code, without having to hide them in variable names
or strings that do nothing. */(int*) = &print_array;
"Here's how I would have had to annotate code before. It's kind of a pain.";
srand((unsigned int)time(NULL));
x ^= x; /* ^= looks like a duck. I'm going to call it the "quack operator" */
for(;((y = rand/*om number generator*/(), x < /* I'm a comment in the middle of an expression */ARRAY_SIZE) && ((((x
= x + 1) - 1)[iarray] = /*number from 0 to 99*/(y - (y / 100) * 100)) || 1));); /* lot's of nested () there! */
PNT2LINES(out, Before sort, ---------------, \n);
for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
[iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y)); /* This used to be two loops, when it was with GOTOs,
but I was able to
reduce it to one. That must be faster, right? It's also still easy to read, so it's a total win-win! */
/* That line had a lot of quack operators! */
PNT2LINES(out, After sort, ---------------, \n);
(*prnt_func)(&ofs + ofs);
}