-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
93 lines (82 loc) · 1.38 KB
/
_printf.c
File metadata and controls
93 lines (82 loc) · 1.38 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "main.h"
/**
* _printf - Produces output according to a format.
* @format: Character string containing directives.
*
* Return: Number of characters printed.
*/
int _printf(const char *format, ...)
{
va_list args;
int printed_chars = 0;
int num, temp, len;
char buffer[11];
int i;
char ch;
char *str;
va_start(args, format);
while (*format)
{
if (*format == '%')
{
format++;
switch (*format)
{
case 'c':
ch = (char) va_arg(args, int);
write(1, &ch, 1);
printed_chars++;
break;
case 's':
str = va_arg(args, char *);
while (*str)
{
write(1, str, 1);
str++;
printed_chars++;
}
break;
case 'd':
case 'i':
num = va_arg(args, int);
len = 0;
if (num < 0)
{
write(1, "-", 1);
printed_chars++;
num *= -1;
}
temp = num;
do {
temp /= 10;
len++;
} while (temp != 0);
for (i = len - 1; i >= 0; i--)
{
buffer[i] = (num % 10) + '0';
num /= 10;
}
write(1, buffer, len);
printed_chars += len;
break;
case '%':
write(1, "%", 1);
printed_chars++;
break;
default:
write(1, "%", 1);
write(1, format, 1);
printed_chars += 2;
break;
}
}
else
{
write(1, format, 1);
printed_chars++;
}
format++;
}
va_end(args);
return (printed_chars);
}