-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
119 lines (111 loc) · 3.7 KB
/
ft_printf.c
File metadata and controls
119 lines (111 loc) · 3.7 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sel-abbo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 17:31:15 by sel-abbo #+# #+# */
/* Updated: 2024/11/22 23:16:41 by sel-abbo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_format(va_list args, const char *frmt)
{
int count;
void *ptr;
count = 0;
if (*frmt == 'c')
count += ft_putchar(va_arg(args, int));
else if (*frmt == 's')
count += ft_putstr(va_arg(args, char *));
else if (*frmt == 'd' || *frmt == 'i')
count += ft_putnbr(va_arg(args, int));
else if (*frmt == 'u')
count += ft_putnbr_unsigned(va_arg(args, unsigned int));
else if (*frmt == 'x')
count += ft_puthexa(va_arg(args, unsigned int), 1);
else if (*frmt == 'X')
count += ft_puthexa(va_arg(args, unsigned int), 0);
else if (*frmt == 'p')
{
ptr = va_arg(args, void *);
if (ptr == NULL)
return (count += ft_putstr("(nil)"), count);
count += ft_putstr("0x");
count += ft_puthexa((unsigned long)ptr, 1);
}
return (count);
}
int ft_printf(const char *frmt, ...)
{
va_list args;
int count;
va_start(args, frmt);
count = 0;
if (!frmt)
return (-1);
while (*frmt)
{
if (*frmt == '%' && *(frmt + 1))
{
frmt++;
if (*frmt == '%')
count += ft_putchar('%');
else
count += ft_format(args, frmt);
}
else
count += ft_putchar(*frmt);
frmt++;
}
va_end(args);
return (count);
}
// int main()
// {
// int x = 42;
// char *long_string = "This is a very long string...";
// freopen("ft_output.txt", "w", stdout);
// ft_printf("Integer: %d\n", 42);
// ft_printf("Negative integer: %d\n", -42);
// ft_printf("Integer with i: %i\n", 0);
// ft_printf("Unsigned: %u\n", 12345);
// ft_printf("Max unsigned: %u\n", 4294967295U);
// ft_printf("Hexadecimal (lower): %x\n", 255);
// ft_printf("Hexadecimal (zero): %x\n", 0);
// ft_printf("Hexadecimal (upper): %X\n", 255);
// ft_printf("Pointer test: %p\n", &x);
// ft_printf("Null pointer: %p\n", NULL);
// ft_printf("Percent: %%\n");
// ft_printf("Mix: %d, %s, %x, %c\n", 42, "Test", 255, 'Z');
// ft_printf("Empty string: '%s'\n", "");
// ft_printf("Zero: %d\n", 0);
// ft_printf("Large int: %d\n", 2147483647);
// ft_printf("Large unsigned: %u\n", 4294967295U);
// ft_printf("%s\n", long_string);
// ft_printf("Nested: %s\n", "Check %x, %d");
// freopen("std_output.txt", "w", stdout);
// printf("Integer: %d\n", 42);
// printf("Negative integer: %d\n", -42);
// printf("Integer with i: %i\n", 0);
// printf("Unsigned: %u\n", 12345);
// printf("Max unsigned: %u\n", 4294967295U);
// printf("Hexadecimal (lower): %x\n", 255);
// printf("Hexadecimal (zero): %x\n", 0);
// printf("Hexadecimal (upper): %X\n", 255);
// printf("Pointer test: %p\n", &x);
// printf("Null pointer: %p\n", NULL);
// printf("Percent: %%\n");
// printf("Mix: %d, %s, %x, %c\n", 42, "Test", 255, 'Z');
// printf("Empty string: '%s'\n", "");
// printf("Zero: %d\n", 0);
// printf("Large int: %d\n", 2147483647);
// printf("Large unsigned: %u\n", 4294967295U);
// printf("%s\n", long_string);
// printf("Nested: %s\n", "Check %x, %d");
// }
/* int main()
{
printf("%d\n", printf(NULL));
} */