Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions holberton.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef __HOLBERTON_H__
#define __HOLBERTON_H__

#include <stdarg.h>
#include <stdlib.h>

/**
* struct printer - format printer struct
* @spec: the format specifier
Expand All @@ -19,8 +22,10 @@ int print_char(va_list ap);
int print_string(va_list ap);
int print_int(va_list ap);
int print_unsigned(va_list ap);
int print_octal(va_list ap);
int print_digit(int num, int *count);
int print_digit_unsigned(unsigned int num, int *count);
int print_digit_octal(unsigned int num, int *count);
int call_print_fn(char ch, print_t funcs[], va_list ap);

#endif
44 changes: 40 additions & 4 deletions print_funcs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <stdarg.h> /* va_list, va_start, va_arg, va_end macros */
#include <stdlib.h> /* NULL macro */
#include <limits.h>
#include "holberton.h" /* _putchar */

Expand Down Expand Up @@ -103,7 +101,7 @@ int print_unsigned(va_list ap)
exit(-1);

*count_ptr = 0;
num = va_arg(ap, int);
num = va_arg(ap, unsigned int);

(*count_ptr) = print_digit_unsigned(num, count_ptr);

Expand All @@ -121,7 +119,45 @@ int print_digit_unsigned(unsigned int num, int *count)
if (num / 10)
print_digit_unsigned(num / 10, count);


(*count) += _putchar((num % 10) + '0');
return (*count);
}


/**
* print_octal - prints an unsigned int octal from va_list
* @ap: va_list object from calling function
* Return: integer count of characters printed
*/
int print_octal(va_list ap)
{
int *count_ptr;
unsigned int num;

count_ptr = malloc(sizeof(*count_ptr));

if (!count_ptr)
exit(-1);

*count_ptr = 0;
num = va_arg(ap, unsigned int);

(*count_ptr) = print_digit_octal(num, count_ptr);

return (*count_ptr);
}

/**
* print_digit_octal - print the digits recursively
* @num: next usigned octal int in the va_arg list
* @count: pointer to integer count digits
* Return: pointer to integer count of character printed
*/
int print_digit_octal(unsigned int num, int *count)
{
if (num / 8)
print_digit_octal(num / 8, count);

(*count) += _putchar((num % 8) + '0');
return (*count);
}
4 changes: 2 additions & 2 deletions printf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <stdarg.h> /* va_list, va_start, va_arg, va_end macros */
#include <stdlib.h> /* NULL macro */
#include "holberton.h" /* _putchar */

/**
Expand All @@ -17,6 +15,7 @@ int _printf(const char *format, ...)
{"d", print_int},
{"i", print_int},
{"u", print_unsigned},
{"o", print_octal},
{NULL, NULL}
};
va_start(ap, format);
Expand All @@ -37,6 +36,7 @@ int _printf(const char *format, ...)
case 'd':
case 'i':
case 'u':
case 'o':
count += call_print_fn(format[i], funcs, ap);
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions tests/small_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int main(void)
int a = 0;
int b = 0;

a = printf("stdio %d\n", INT_MIN);
b = _printf("ours %d\n", INT_MIN);
a = printf("stdio %o\n", 0);
b = _printf("ours %o\n", 0);
/* printf("%d", a);*/
printf("(%d, %d)\n", a, b);

Expand Down