-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_functions.c
executable file
·124 lines (101 loc) · 1.88 KB
/
print_functions.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
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
120
121
122
123
124
#include "main.h"
#include <unistd.h>
#include <stdio.h>
/**
* print_char - writes the character c to stdout
* @arg: argument
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int print_char(va_list arg)
{
return (_putchar(va_arg(arg, int)));
}
/**
* print_int - prints an integer.
* @arg: argument
* Return: 0
*/
int print_int(va_list arg)
{
unsigned int divisor = 1, i, resp, charPrinted = 0;
int n = va_arg(arg, int);
if (n < 0)
{
_putchar('-');
charPrinted++;
n *= -1;
}
for (i = 0; n / divisor > 9; i++, divisor *= 10)
;
for (; divisor >= 1; n %= divisor, divisor /= 10, charPrinted++)
{
resp = n / divisor;
_putchar('0' + resp);
}
return (charPrinted);
}
/**
* print_STR - prints a string with a `S` (upper case) specificer
* @arg: argument
* Return: number of character printed
*/
int print_STR(va_list arg)
{
int i;
char *str = va_arg(arg, char*);
if (str == NULL)
str = "(null)";
else if (*str == '\0')
return (-1);
for (i = 0; str[i]; i++)
{
if ((str[i] < 32 && str[i] > 0) || str[i] >= 127)
{
_putchar('\\');
_putchar('x');
if (i < 16)
_putchar('0');
print_unsignedIntToHex(str[i], 'A');
}
else
_putchar(str[i]);
}
return (i);
}
/**
* print_str - prints a string with a `s` (lower case) specifier
* @arg: argument
* Return: number of character printed
*/
int print_str(va_list arg)
{
int i;
char *str = va_arg(arg, char*);
if (str == NULL)
str = "(null)";
else if (*str == '\0')
return (-1);
for (i = 0; str[i]; i++)
_putchar(str[i]);
return (i);
}
/**
* print_unsigned - prints an unsigned int.
* @arg: argument
* Return: 0
*/
int print_unsigned(va_list arg)
{
int divisor = 1, i, resp;
unsigned int n = va_arg(arg, unsigned int);
for (i = 0; n / divisor > 9; i++, divisor *= 10)
;
for (; divisor >= 1; n %= divisor, divisor /= 10)
{
resp = n / divisor;
_putchar('0' + resp);
}
return (i + 1);
}