Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active October 30, 2020 23:22
Show Gist options
  • Select an option

  • Save hidsh/e9e86e11e4798a20964d1ef71be9e746 to your computer and use it in GitHub Desktop.

Select an option

Save hidsh/e9e86e11e4798a20964d1ef71be9e746 to your computer and use it in GitHub Desktop.
C test: var-args, like printf()
❯❯❯ gcc variable_length_arg_func.c && ./a.out
var:01
var1:11 var2:12
var1:F1 var2:F2 var3:F3
#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