#include <unistd.h>
void ft_putnbr(int nb)
{
long nbl; // long int
char c[10];
short i;
nbl = nb; // Paste our input (nb) into our long int, nbl.
i = 0;
if (0 == nb) // check for an input of 0, if so we are done!
{
write(1, "0", 1);
return ;
}
if (nbl < 0) // Check for negative input.
// if so, write the negative sign.
{
nbl *= -1; // change the sign of the value stored in nbl.
write(1, "-", 1);
}
// Repeatedly extracts the rightmost digit of nbl, stores its ASCII
// value in c, and removes the digit from nbl.
// This continues until all digits have been processed and nbl becomes 0.
while (nbl)
{
c[i++] = (nbl % 10) + 48;
nbl /= 10;
}
// Loop backwards over the array
while (i > 0)
write(1, &c[--i], 1);
}
int main(void)
{
ft_putnbr(-42);
write(1, "\n", 1);
ft_putnbr(0);
write(1, "\n", 1);
ft_putnbr(42);
write(1, "\n", 1);
ft_putnbr(2147483647);
write(1, "\n", 1);
ft_putnbr(-2147483648);
write(1, "\n", 1);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: