-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Variadic function mandatory tasks complete
- Loading branch information
1 parent
e9a227c
commit 9d5c67a
Showing
7 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |