How can I write a basic function using just the write
function declared in #include <unistd.h>
, in order to display a number?
example:
ft_putnbr.c:
int ft_putnbr(int nbr)
{
write(1, &nbr, sizeof(int));
return (0);
}
int ft_putchar(char c)
{
write(1, &c, 1);
return (0);
}
main.c:
int ft_putnbr(int nbr);
int ft_putchar(char c);
int main(void)
{
ft_putnbr(6);
ft_putchar('\n');
ft_putchar('a');
return (0);
}
The function ft_putchar
works fine, but I would like to do the same with a number, with my ft_putnbr
function.
There is no error when I'm compiling or executing the code, but a blank is displayed instead of my number.
i use this one:
void ft_putnbr(int nbr)
{
if (nbr < 0)
{
ft_putchar('-');
ft_putnbr(-nbr);
}
else if (nbr > 9)
{
ft_putnbr(nbr / 10);
ft_putnbr(nbr % 10);
}
else
{
ft_putchar(nbr + '0');
}
}