-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
108 lines (85 loc) · 1.46 KB
/
functions.c
File metadata and controls
108 lines (85 loc) · 1.46 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
#include <stdarg.h>
#include <unistd.h>
#include "main.h"
/**
* print_char - Prints a single character.
* @args: va_list containing the character to print.
*
* Return: Number of characters printed (1).
*/
int print_char(va_list args)
{
char c = va_arg(args, int);
return (write(1, &c, 1));
}
/**
* print_string - Prints a string from the argument list.
* @args: va_list containing the string to print.
*
* Return: Number of characters printed.
*/
int print_string(va_list args)
{
char *str = va_arg(args, char *);
int count = 0;
if (str == NULL)
str = "(null)";
while (str[count])
{
write(1, &str[count], 1);
count++;
}
return (count);
}
/**
* print_percent - Prints a percent sign.
* @args: va_list (unused).
*
* Return: Number of characters printed (always 1).
*/
int print_percent(va_list args)
{
(void)args;
write(1, "%", 1);
return (1);
}
/**
* print_integer - Prints an integer to stdout
* @args: The list of variadic arguments
*
* Return: Number of characters printed
*/
int print_integer(va_list args)
{
int n = va_arg(args, int);
int num, last_digit, count = 0;
char c;
char buffer[20];
int i = 0;
if (n < 0)
{
c = '-';
write(1, &c, 1);
count++;
n = -n;
}
num = n;
if (num == 0)
{
c = '0';
write(1, &c, 1);
return (count + 1);
}
while (num > 0)
{
last_digit = num % 10;
buffer[i++] = '0' + last_digit;
num /= 10;
}
while (i--)
{
write(1, &buffer[i], 1);
count++;
}
return (count);
}