Q:

printf() function not working

i have created a c program to print all possible strings of 8 characters. But the printf() function inside the program does not work at all. Here's the program
#include <stdio.h>
#include <string.h>
#include <conio.h>
void ini(void);
void check(void);
void add(void);
static char str[9]; //permuting string
static char test[9]; //test string
static short int c = 1;
void print(char*);
void print(char a[]) {
    short int i,ln = (int)(strlen(str)-1);
    for(i = 0; i <= ln; i++)
    printf("%c",a[i]);
    printf("n");
}
void ini() {
    //initialzing the strings.
    short int i = 0;
    for(i = 0; i < 9; i++){
        str[i] = 'A';
        test[i]= 'z';
    }
    puts("ini.....");
    //initializing done
}
void check() {
    //check if the strings 'str' and 'test' are equal
    c = strcmp(str, test);
    puts("checking..........");
}
void add() {
    //this is the heart of the program
    short int i = 0;
    for( i = 7; i > 0; i--)
    {
        if((int)str[i] >= (int)'z') {
            str[i] = 'A';
            str[i-1]++;
        }
        else if(str[i] < 'z'){
            str[i]++;
            break;
        }
    }
    puts("adding.......");
}
int main() {
    //now we execute the functions above
    puts("in main.....");
    int i = 0;
    while( c != 0 ) //execute the loop while the permuting string 'str' is not equal to the final string 'test'
    {
        puts("inside while.......");
        for(i = 65; i <= 122; i++) { //loop to make the last character of 'str' permute from 'a' to'Z'
            str[7] = (char)i;
            puts("inside for");
            print(str); //print the whole string to the screen
        }
        add(); //change the next char
        check(); //check to see if 'str' has reached it's final point.
    }
    return 1;
    getch();
}

and here's the result.............

The program enters the for loop in main() but it does not execute the print() function. I have tried printf() but that shows the same result. What am I doing wrong?

A:

You decleared, 
static char str[9]; 

So, all elements in str is NULL (\0) now. Then,
ln = (int)(strlen(str)-1);

Here, strlen counts characters until a \0 found. So, as the first character in str is \0, ln will be -1. In the for loop,
for(i = 0; i <= ln; i++)

i <= ln condition will fail.
You may try, in the main function,
str[0] = (char) i; 

or, call ini() before calling print(str);
But careful, your while loop will never break! As c's never gonna be 0.
Because your code does not changing the 9th character from A to z so strcmp(str, test) is not going to return 0.

