-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
52 lines (47 loc) · 1.83 KB
/
ft_putnbr_fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgasc <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 14:34:57 by lgasc #+# #+# */
/* Updated: 2023/06/29 19:12:55 by lgasc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static unsigned int power(const unsigned int base, const size_t exponent);
/** Outputs the integer `integer` to the given `file_descriptor`.
* @param[in] integer The integer to output.
* @param[in] file_descriptor The file descriptor on which to write.
* @remarks External function: `write`
*/
void ft_putnbr_fd(int integer, int file_descriptor)
{
size_t length;
unsigned int i;
signed char sign;
sign = 1;
if (integer < 0)
{
sign = -1;
write(file_descriptor, "-", 1);
}
length = 1;
while (integer / (const signed int) power(10, length - 1) <= -10
|| integer / (const signed int) power(10, length - 1) >= 10)
length++;
i = 0;
while (i < length)
{
write(file_descriptor, & "0123456789"[(integer / (const signed int)
power(10, length - 1 - i)) % 10 * sign], 1);
i++;
}
}
static unsigned int power(const unsigned int base, const size_t exponent)
{
if (exponent == 0)
return (1);
return (base * power(base, exponent - 1));
}