Search code examples
cunixsystemcall

Why my ft_print_comb function does not give any output?


#include<stdio.h>   
#include<unistd.h>

void ft_putchar(char x){
    write(1, &x, 1);
}
void ft_print_comb()
{
    char i, j, k;
    i = '0';
    while(i <= 7){
        i++;
        j = i+1;
        while(j <= 8){
            j++;
        
            k = j+1;
            while(k <= 9){
                k++;
                    ft_putchar(i);
                    ft_putchar(j);
                    ft_putchar(k);
                    ft_putchar(',');
                    ft_putchar(' ');
            }
            }
        }
}
int main(){
    ft_print_comb();
    return 0;
}

I have tried to do couple changes but it either broke the code or kept giving me no output. What I am trying to do is create a function that displays all different combinations of three different digits in ascending order, listed by ascending order. for loop and printf functions are not allowed.


Solution

  • Since you are using chars, you should compare with character literals instead of integers. For instance, the while loop is never entered because the ASCII code for '0' is 48, which is greater than 7.

    while (i <= '7') {
        j = i + 1;
        while (j <= '8') {
            k = j + 1;
            while (k <= '9') {
                ft_putchar(i);
                ft_putchar(j);
                ft_putchar(k);
                ft_putchar(',');
                ft_putchar(' ');
                k++;
            }
            j++;
        }
        i++;
    }