Search code examples
cprintfprogram-entry-point

C - Triangle program not printing any output on call to main


I have written a C code that is supposed to print a triangle. A width and character are defined and the code should then print out a triangle of that with a fill of that character. I think there may be an error in main, but the code compiles fine. When I run it, there is no output.

Here is my code:

void triangle(int width, char x);

int main(void){
    triangle(4, c);
}

void triangle(int width, char x){
    if (width > 2){
        return;
    }
    int counter = 1;
    int direction = 1;
    do{
        int i;
        for(i = 0; i < width - counter; i++){
            printf(" ");
        }
        for (int i = 0; i < counter; i++){
            printf("%c", x);
        }
        printf("\n");
        counter += direction;
        if(counter > width){
            counter = width - 1;
            direction = -1;
        }
    }while (counter != 1);
    return;
}

Solution

  • Sorry if this wasn't clear in the question. The expected output should be a triangle of width (and height) of 4, with a fill of 'c'.

    So I think I have figured it out. I must use the < operator (silly human error) and the character must have a single quote: 'c'.