forked from Brexblime/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_octal.c
More file actions
41 lines (38 loc) · 681 Bytes
/
Copy pathprint_octal.c
File metadata and controls
41 lines (38 loc) · 681 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
#include "main.h"
/**
* print_octal - prints an unsigned integer in octal notation
* @arg: argument list containing the unsigned integer to print
* Return: the number of characters printed
*/
int print_octal(va_list arg)
{
unsigned int num = va_arg(arg, unsigned int);
unsigned int num_copy = num;
int len = 0;
char *str;
if (num == 0)
return (_putchar('0'));
while (num_copy != 0)
{
num_copy /= 8;
len++;
}
str = malloc(sizeof(char) * len);
if (str == NULL)
return (-1);
len--;
while (num != 0)
{
str[len] = (num % 8) + '0';
num /= 8;
len--;
}
len = 0;
while (str[len] != '\0')
{
_putchar(str[len]);
len++;
}
free(str);
return (len);
}