forked from henrychris/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoctal.c
More file actions
48 lines (42 loc) · 742 Bytes
/
octal.c
File metadata and controls
48 lines (42 loc) · 742 Bytes
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
#include "main.h"
/**
* print_octal - prints an unsigned int in octal
* @num: number to be printed
* Return: number of characters printed
*/
int print_octal(unsigned int num)
{
char *str;
str = conv_octal(num);
if (str == NULL)
return (-1);
return (print_str(str));
}
/**
* conv_octal - converts a number to octal
* @num: the number to be converted
* Return: nothing
*/
char *conv_octal(unsigned int num)
{
unsigned int temp = num;
int digits = 0, i;
char *str;
while (temp)
{
temp /= 8;
digits++;
}
str = malloc((digits + 1) * sizeof(char));
if (str == NULL)
{
return (NULL);
}
for (i = 0; i < digits; i++)
{
str[digits - i - 1] = (num % 8) + '0';
num /= 8;
}
str[digits] = '\0';
return (str);
}