Skip to content

Commit

Permalink
last task
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakhinamammadzade committed Nov 26, 2023
1 parent 67dfa09 commit c3e942f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions variadic_functions/3-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "variadic_functions.h"

/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
print_all("ceis", 'B', 3, "stSchool");
return (0);
}
83 changes: 83 additions & 0 deletions variadic_functions/3-print_all.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "variadic_functions.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

/**
* print_string - Print the string
* @args: Where the string is
*/
void print_string(va_list args)
{
char *string;

string = va_arg(args, char *);
if (string == NULL)
string = "(nil)";
printf("%s", string);
}

/**
* print_char - Print the char
* @args: Where the char is
*/
void print_char(va_list args)
{
printf("%c", va_arg(args, int));
}

/**
* print_int - Print the int
* @args: Where the int is
*/
void print_int(va_list args)
{
printf("%d", va_arg(args, int));
}

/**
* print_float - Print the float
* @args: Where the float is
*/
void print_float(va_list args)
{
printf("%f", va_arg(args, double));
}

/**
* print_all - Print any type of given argument
* @format: List of types of arguments being passed
*/
void print_all(const char * const format, ...)
{
va_list args;
char *sep;
print_form_t print_form[] = {
{"c", print_char},
{"i", print_int},
{"f", print_float},
{"s", print_string}
};
int i, j;

va_start(args, format);
i = 0;
sep = "";
while (format != NULL && format[i] != '\0')
{
j = 0;
while (j < 4)
{
if (*print_form[j].c == format[i])
{
printf("%s", sep);
print_form[j].f(args, sep);
sep = ", ";
}
j++;
}
i++;
}
printf("\n");
va_end(args);
}

0 comments on commit c3e942f

Please sign in to comment.