There's a much better way to calculate permutations (which isn't exactly what you're doing, but w.e) than using a system of nested for loops.

You'll first need to set up a few variables:

Code:

int size_set = 2;
char *chrset = "ab";
int set_len = 4;
char res[set_len + 1];
char *tmp = res;
int n, pos, permc, perm_per_len, wordlen;

Then, calculate how many permutations you'll actually need:

Code:

perm_per_len = 1;
for (n = 0; n < set_len; n++)
perm_per_len *= size_set;

Finally, generate the actual permutations:

Code:

for (n = 0; n < perm_per_len; n++) {
permc = 1;
tmp = res;
for (wordlen = 0; wordlen < set_len; wordlen++) {
pos = (n / permc) % size_set;
*tmp++ = chrset[pos];
permc *= size_set;
}
printf("%s\n", res);
}

Don't go and copy my code, but use it as an example.