-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf_int.c
65 lines (62 loc) · 1.23 KB
/
_printf_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
54
55
56
57
58
59
60
61
62
63
64
65
#include "main.h"
/**
* add_flag - adds a flag to the number
* @f: pointer to flag struct
* Return: 1(for add flag)
*/
int add_flag(flag *f)
{
if (f->plus)
return (_putchar('+'));
else if (f->space)
return (_putchar(' '));
return (0);
}
/**
* _print_int - Function to print integer
* @argPtr: argument Pointer
* @f: pointer to flag struct
* Return: counter(number of printed characters)
*/
int _print_int(va_list argPtr, flag *f)
{
long int n;
int count = 0, i = 0, padding, is_negative = 0;
unsigned long int num;
char buffer[20];
if (f->length == 'l')
n = va_arg(argPtr, long int);
else if (f->length == 'h')
n = (short int)va_arg(argPtr, int);
else
n = va_arg(argPtr, int);
if (n == 0)
{
buffer[i++] = '0';
num = n;
}
else if (n < 0)
{
is_negative = 1;
num = (unsigned long int)(-n);
}
else
num = (unsigned long int)n;
while (num > 0)
{
buffer[i++] = num % 10 + '0';
num /= 10;
}
padding = f->width - i - (is_negative || f->plus || f->space ? 1 : 0);
if (!f->isLeft)
count += _print_padding(padding);
if (is_negative)
count += _putchar('-');
else
count += add_flag(f);
while (i > 0)
count += _putchar(buffer[--i]);
if (f->isLeft)
count += _print_padding(padding);
return (count);
}