Created
September 16, 2017 15:36
-
-
Save fallenleavesguy/55a6b60371d712465b077b1bf300ea96 to your computer and use it in GitHub Desktop.
variable-arguments in c
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 <stdarg.h> | |
#include <stdio.h> | |
void minprintf(char *fmt, ...); | |
void minprintf(char *fmt, ...) { | |
va_list ap; | |
char *p, *sval; | |
int ival; | |
double dval; | |
va_start(ap, fmt); | |
// | |
for (p = fmt; *p; p++) { | |
if (*p !='%') { | |
putchar(*p); | |
continue; | |
} | |
switch (*++p) { | |
case 'd': | |
ival = va_arg(ap, int); | |
printf("%d", ival); | |
break; | |
case 'f': | |
dval = va_arg(ap, double); | |
printf("%f", dval); | |
break; | |
case 's': | |
for (sval = va_arg(ap, char *); *sval; sval++) | |
putchar(*sval); | |
break; | |
default: | |
putchar(*p); | |
break; | |
} | |
} | |
va_end(ap); | |
} | |
int main(int argc, char *argv[]) { | |
minprintf("%d %f %s\n", 55, 23.4, "variable-arguments.c"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment