-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
51 lines (49 loc) · 1.34 KB
/
_printf.c
File metadata and controls
51 lines (49 loc) · 1.34 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
#include "main.h"
/**
* _printf - prints anything
* @format: list of types of arguments
* passed to the function
* Return: void
*/
int _printf(const char *format, ...)
{
va_list args;
int count = 0, i;
if (format == NULL)
return (-1);
va_start(args, format);
for (i = 0; format && format[i] != '\0'; i++)
{
if (format[i] != '%')
count += print_char(format[i]);
else
{
if (format[i + 1] == 'c')
count += print_char(va_arg(args, int));
else if (format[i + 1] == '%')
count += print_char('%');
else if (format[i + 1] == 'i' || format[i + 1] == 'd')
count += print_int(va_arg(args, int));
else if (format[i + 1] == 'u')
count += print_unsgn(va_arg(args, unsigned int));
else if (format[i + 1] == 'b')
count += print_binary(va_arg(args, int));
else if (format[i + 1] == 'S' || format[i + 1] == 's')
count += get_str_print(va_arg(args, char *), format[i + 1]);
else if (format[i + 1] == 'o')
count += print_octal(va_arg(args, int));
else if (format[i + 1] == 'x' || format[i + 1] == 'X')
count += print_hex(va_arg(args, int), format[i + 1]);
else if (format[i + 1] != '%')
{
count += print_char('%');
count += print_char(format[i + 1]);
}
else if (!format[i + 1] || (format[i + 1] == ' ' && !format[i + 2]))
return (-1);
i++;
}
}
va_end(args);
return (count);
}