-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_octal_digits.c
More file actions
executable file
·71 lines (54 loc) · 1.07 KB
/
Copy pathprintf_octal_digits.c
File metadata and controls
executable file
·71 lines (54 loc) · 1.07 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
#include "main.h"
/**
* printf_oct: Prints octal representation of an unsigned integer
* @par: Arguments
* Return: Count of characters printed
*/
int printf_oct(va_list par) {
unsigned int digit;
int counter = 0;
int *array;
int i;
unsigned int temp;
digit= va_arg(par, unsigned int);
/**
* Calculate the number of octal digits required
**/
temp = digit;
while (temp > 0) {
temp /= 8;
counter++;
}
/**
* Allocate memory for an array to store octal digits
**/
array= malloc(counter * sizeof(int));
if (array == NULL) {
return 0;
/**
* Handle memory allocation failure
**/
}
/**
* Populate the array with the octal digits
**/
temp = digit;
for (i = 0; i < counter; i++) {
array[i] = temp % 8;
temp /= 8;
}
/**
* Print the octal digits in reverse order
*/
for (i = counter - 1; i >= 0; i--) {
_putchar(array[i] + '0');
}
/**
* Free the allocated memory for the array
*/
free(array);
/**
* Return the total count¢ of octal digits printed
*/
return counter;
}