-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnk_printf.c
More file actions
66 lines (61 loc) · 1.13 KB
/
nk_printf.c
File metadata and controls
66 lines (61 loc) · 1.13 KB
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
66
#include "main.h"
/* Function prototypes */
int nk_print_char(va_list args);
int nk_print_str(va_list args);
int print_pct(void);
int print_int(va_list args);
int print_unsigned_int_binary(va_list args);
/**
* _printf - custom printf function
* @format: format specifier
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
va_list args;
int printed_chars = 0;
if (!format)
return (-1);
va_start(args, format);
while (*format)
{
if (*format == '%')
{
format++;
if (*format == '\0')
return (-1);
switch (*format)
{
case 'c':
printed_chars += nk_print_char(args);
break;
case 's':
printed_chars += nk_print_str(args);
break;
case '%':
printed_chars += print_pct();
break;
case 'd':
case 'i':
printed_chars += print_int(args);
break;
case 'b':
printed_chars += print_unsigned_int_binary(args);
break;
default:
_putchar('%');
_putchar(*format);
printed_chars += 2;
break;
}
}
else
{
_putchar(*format);
printed_chars++;
}
format++;
}
va_end(args);
return (printed_chars);
}