-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_output_int.c
53 lines (48 loc) · 860 Bytes
/
_output_int.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
53
#include "main.h"
/**
* _print_int - a function that prints an integer to stdout
* Return: the number of characters printed
* @num: the list of arguments
*/
int _print_int(int num)
{
int is_negative = num < 0 ? 1 : 0;
unsigned int num_u = is_negative ? -num : num;
char *str;
int i, j, k, len = 0;
char tmp;
/* the number of digits are counted*/
i = num_u;
while (i > 0)
{
len++;
i /= 10;
}
/* add an extra digit*/
if (is_negative)
{
len++;
}
str = malloc(len + 1);
/* convert the number to a string */
for (i = num_u, j = 0; i > 0; i /= 10, j++)
{
str[j] = i % 10 + '0';
}
if (is_negative)
{
str[j++] = '-';
}
str[j] = '\0';
/*the string is reversed*/
for (k = 0, j--; k < j; k++, j--)
{
tmp = str[k];
str[k] = str[j];
str[j] = tmp;
}
/* print the string */
len = _puts(str);
free(str);
return (len);
}