Last active
October 30, 2020 23:22
-
-
Save hidsh/e9e86e11e4798a20964d1ef71be9e746 to your computer and use it in GitHub Desktop.
C test: var-args, like printf()
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
| ❯❯❯ gcc variable_length_arg_func.c && ./a.out | |
| var:01 | |
| var1:11 var2:12 | |
| var1:F1 var2:F2 var3:F3 |
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 <stdio.h> | |
| #include <stdarg.h> | |
| void my_printf(const char* fmt, ...) | |
| { | |
| char buf[100]; | |
| va_list ap; | |
| va_start(ap, fmt); | |
| vsprintf(buf, fmt, ap); | |
| va_end(ap); | |
| puts(buf); | |
| } | |
| int main() | |
| { | |
| my_printf("var:%02X\n", 0x01); | |
| my_printf("var1:%02X var2:%02X\n", 0x11, 0x12); | |
| my_printf("var1:%02X var2:%02X var3:%02X\n", 0xf1, 0xf2, 0xf3); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment