-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
65 lines (57 loc) · 968 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "main.h"
/**
* _printf - produces ouput according to a format
* @format: a character string
*
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
int char_count = 0;
char c;
char *str;
va_list argument_list;
va_start(argument_list, format);
while (*format)
{
if (*format == '%')
{
format++;
if (*format == '\0')
return (-1);
if (*format == 'c')
{
c = va_arg(argument_list, int);
write(1, &c, 1);
char_count++;
}
else if (*format == 's')
{
str = va_arg(argument_list, char *);
while (*str)
{
write(1, str, 1);
str++;
char_count++;
}
}
else if (*format == '%')
{
write(1, "%", 1);
char_count++;
}
else if (*format == 'd' || *format == 'i')
{
char_count += print_int(argument_list);
}
}
else
{
write(1, format, 1);
char_count++;
}
format++;
}
va_end(argument_list);
return (char_count);
}