-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf_sub_functs.c
86 lines (82 loc) · 1.34 KB
/
_printf_sub_functs.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
#include "main.h"
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
/**
* _printf_char - prints character.
* @c: char.
* Return: number of bytes written.
*/
int _printf_char(char c)
{
return (write(1, &c, 1));
}
/**
* _printf_string - prints string.
* @str: string.
* Return: number of bytes written.
*/
int _printf_string(char *str)
{
int length = 0;
if (str == NULL)
str = "(null)";
for (length = 0; str[length] != '\0'; length++)
continue;
return (write(1, str, length));
}
/**
* _printf_int - prints int.
* @a: integer.
* Return: Count.
*/
int _printf_int(int a)
{
unsigned int b, count = 0, i;
if (a < 0)
{
count += _printf_char('-');
b = -1 * a;
}
else
b = a;
for (i = 1; (b / i) / 10 > 0; i *= 10)
continue;
for (; i > 0; i /= 10)
{
count += _printf_char(b / i + 48);
b = b % i;
}
return (count);
}
/**
* _printf_binary - prints binary.
* @a: int.
* Return: count.
*/
int _printf_binary(long int a)
{
int is_negative = 0, count = 0;
long int i = 1;
if (a < 0)
{
is_negative = 1;
a = -1 * (a + 1);
}
for (; (a / i) / 2 != 0; i *= 2)
continue;
count += _printf_char(a / i + 48);
for (; i >= 2; i /= 2)
{
if (is_negative == 0)
count += _printf_char(a % i + 48);
else
{
if (a % i == 0)
count += _printf_char('1');
else
count += _printf_char('0');
}
}
return (count);
}