Skip to content

Commit

Permalink
Variadic function mandatory tasks complete
Browse files Browse the repository at this point in the history
  • Loading branch information
EphraimTM299 committed Jan 20, 2023
1 parent e9a227c commit 9d5c67a
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 0x10-variadic_functions/0-sum_them_all.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdarg.h>
#include "variadic_functions.h"

/**
* sum_them_all - sums of all its parameters.
* @n: number of parameters
*
* Return: sum of all parameters
*/
int sum_them_all(const unsigned int n, ...)
{
int sum = 0;
unsigned int i;
va_list valist;

va_start(valist, n);
for (i = 0; i < n; i++)
sum += va_arg(valist, int);
va_end(valist);
return (sum);
}
28 changes: 28 additions & 0 deletions 0x10-variadic_functions/1-print_numbers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stdio.h>
#include "variadic_functions.h"

/**
* print_numbers - prints numbers, followed by a new line.
* @separator: separator to print between numbers
* @n: number of numbers to print
*
* Return: void
*/
void print_numbers(const char *separator, const unsigned int n, ...)
{
unsigned int i;
int num;
va_list valist;

va_start(valist, n);
for (i = 0; i < n; i++)
{
num = va_arg(valist, int);
printf("%d", num);
if (i < n - 1 && separator)
printf("%s", separator);
}
printf("\n");
va_end(valist);
}
31 changes: 31 additions & 0 deletions 0x10-variadic_functions/2-print_strings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdarg.h>
#include <stdio.h>
#include "variadic_functions.h"

/**
* print_strings - prints strings, followed by a new line.
* @separator: separator to print between the strings
* @n: number of strings to print
*
* Return: void
*/
void print_strings(const char *separator, const unsigned int n, ...)
{
unsigned int i;
char *str;
va_list valist;

va_start(valist, n);
for (i = 0; i < n; i++)
{
str = va_arg(valist, char *);
if (str)
printf("%s", str);
else
printf("(nil)");
if (i < n - 1 && separator)
printf("%s", separator);
}
printf("\n");
va_end(valist);
}
92 changes: 92 additions & 0 deletions 0x10-variadic_functions/3-print_all.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdarg.h>
#include <stdio.h>
#include "variadic_functions.h"

/**
* print_c - print a char
* @c: char to print
*
* Return: void
*/
void print_c(va_list c)
{
printf("%c", va_arg(c, int));
}

/**
* print_s - prints a string
* @s: string to print
*
* Return: void
*/
void print_s(va_list s)
{
char *str = va_arg(s, char *);

if (str == NULL)
str = "(nil)";
printf("%s", str);
}

/**
* print_i - prints an int
* @i: int to print
*
* Return: void
*/
void print_i(va_list i)
{
printf("%d", va_arg(i, int));
}

/**
* print_f - prints a float
* @f: float to print
*
* Return: void
*/
void print_f(va_list f)
{
printf("%f", va_arg(f, double));
}

/**
* print_all - prints anything
* @format: list of argument types passed to the function
*
* Return: void
*/
void print_all(const char * const format, ...)
{
unsigned int i, j;
print_t p[] = {
{"c", print_c},
{"s", print_s},
{"i", print_i},
{"f", print_f},
{NULL, NULL}
};
va_list valist;
char *separator = "";

va_start(valist, format);
i = 0;
while (format && format[i])
{
j = 0;
while (p[j].t != NULL)
{
if (*(p[j].t) == format[i])
{
printf("%s", separator);
p[j].f(valist);
separator = ", ";
break;
}
j++;
}
i++;
}
va_end(valist);
printf("\n");
}
8 changes: 8 additions & 0 deletions 0x10-variadic_functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Learning Objectives
At the end of this project, you are expected to be able to explain to anyone, without the help of Google:

General
What are variadic functions
How to use va_start, va_arg and va_end macros
Why and how to use the const type qualifier

13 changes: 13 additions & 0 deletions 0x10-variadic_functions/_putchar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <unistd.h>

/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
21 changes: 21 additions & 0 deletions 0x10-variadic_functions/variadic_functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _VARIADIC_FUNCTIONS_H_
#define _VARIADIC_FUNCTIONS_H_

#include <stdarg.h>
/**
* struct print - print type with corresponding print function
* @t: print type
* @f: print function
*/
typedef struct print
{
char *t;
void (*f)(va_list);
} print_t;

int sum_them_all(const unsigned int n, ...);
void print_numbers(const char *separator, const unsigned int n, ...);
void print_strings(const char *separator, const unsigned int n, ...);
void print_all(const char * const format, ...);

#endif /* _VARIADIC_FUNCTIONS_H_ */

0 comments on commit 9d5c67a

Please sign in to comment.