Skip to content

Commit 963865b

Browse files
committed
add edge case in 0-sum_them_all and add task 1-print_numbers
1 parent f47d1a3 commit 963865b

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

0x10-variadic_functions/0-sum_them_all.c

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ int sum_them_all(const unsigned int n, ...)
1212
unsigned int index, result = 0;
1313
va_list ptr;
1414

15+
if (n == 0)
16+
return (0);
17+
1518
va_start(ptr, n);
1619

1720
for (index = 0; index < n; index++)

0x10-variadic_functions/1-main.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "variadic_functions.h"
2+
3+
/**
4+
* main - check the code
5+
*
6+
* Return: Always 0.
7+
*/
8+
int main(void)
9+
{
10+
print_numbers(", ", 4, 0, 98, -1024, 402);
11+
return (0);
12+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "variadic_functions.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* print_numbers - prints numbers, followed by a new line.
6+
*
7+
* @separator: numbers' separator
8+
* @n: number of args.
9+
*
10+
* Return: Nothing.
11+
*/
12+
void print_numbers(const char *separator, const unsigned int n, ...)
13+
{
14+
unsigned int index;
15+
va_list ptr;
16+
17+
if (separator == NULL)
18+
return;
19+
20+
va_start(ptr, n);
21+
for (index = 0; index < n; index++)
22+
{
23+
printf("%i", va_arg(ptr, int));
24+
if (index != n - 1)
25+
printf("%s", separator);
26+
}
27+
28+
printf("\n");
29+
}

0x10-variadic_functions/variadic_functions.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#include <stdarg.h>
44

55
int sum_them_all(const unsigned int, ...);
6+
void print_numbers(const char *, const unsigned int, ...);
67

78
#endif /* VARIADIC_FUNCTIONS_H */

0 commit comments

Comments
 (0)