-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
50 lines (47 loc) · 905 Bytes
/
_printf.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
#include "main.h"
/**
*_printf - prints to output according to format
*@format: character string containing format specification
*
*Return: number of characters printed or -1 if format NULL
*/
int _printf(const char *format, ...)
{
va_list list;
int i = 0;
int ctr = 0; /* counter */
int (*p_display)(va_list list);
va_start(list, format);
if (format == NULL)
return (-1);
while (format[i])
{
if (format[i] != '%')
{
_putchar (format[i]);
i++;
ctr++;
continue;
}
i++;
p_display = is_char_format(format[i]); /* get format specifier function */
if (p_display != NULL)
ctr += p_display(list); /* call function and update the counter */
else
{
if (format[i] == '\0')
return (-1);
if (format[i] == '%')
ctr += _putchar('%');
else
{
_putchar('%');
_putchar(format[i]);
ctr += 2;
}
}
i++;
}
va_end(list);
return (ctr);
}