Created
February 12, 2019 19:13
-
-
Save rava-dosa/f551769e632dcd57d10a2c53019c4b3e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdlib.h> | |
#include <stdarg.h> | |
#include <stdio.h> | |
void foo(char *fmt, ...) | |
{ | |
va_list ap; | |
int d; | |
char c, *s; | |
va_start(ap, fmt); | |
while (*fmt) { | |
switch (*(fmt++)) { | |
case 's': | |
s = va_arg(ap, char *); | |
printf("string %s\n", s); | |
break; | |
case 'd': | |
d = va_arg(ap, int); | |
printf("int %d\n", d); | |
break; | |
case 'c': | |
/* need a cast here since va_arg only | |
takes fully promoted types */ | |
c = (char) va_arg(ap, int); | |
printf("char %c\n", c); | |
break; | |
} | |
} | |
va_end(ap); | |
} | |
int main(void) | |
{ | |
char *s1 = "First"; | |
char *s2 = "Second"; | |
int d = 42; | |
char c = '?'; | |
foo("sdcs", s1, d, c, s2); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment