I need to print all binary string of length N counting from 0 to the highest number that can represented. My code below works for generating all of the strings. The problem I am having is that the strings are not in order from least to greatest.
#define MAX_LENGTH 10
char binarystrings[MAX_LENGTH];
void binary(int n) {
if (n == 0) {
printf("%s\n", binarystrings);
} else {
binarystrings[n-1] = '0';
binary(n-1);
binarystrings[n-1] = '1';
binary(n-1);
}
}
int main() {
int length;
scanf("%d", &length);
binary(length);
}
When my program takes in 3 it generates:
000
100
010
110
001
101
011
111
But I need it to generate:
000
001
010
011
100
101
110
111
How should I go about making my program put the numbers out in the right order?