Skip to content

Instantly share code, notes, and snippets.

@jq2
Created March 2, 2018 08:08
Show Gist options
  • Select an option

  • Save jq2/7ade55451b42c0693be45126715d3a1b to your computer and use it in GitHub Desktop.

Select an option

Save jq2/7ade55451b42c0693be45126715d3a1b to your computer and use it in GitHub Desktop.
simple-var-args-usage
/*
* @filename: va_args.c
* @author: jq2
* @description: Simple va_list, va_start, va_end usage example...
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
char *read_bi(char *value);
char *read_bj(char value[10]);
void test_read_bi();
void bprint(char *template, ...);
int
main (int argc, char *argv[]) {
printf("Starting program...\n");
/* read_bX tests... */
/* test_read_bi(); */
/* Starting va_args tests... */
char *vla;
bprint("teste", "abc", "aaa", NULL);
return EXIT_SUCCESS;
}
void test_read_bi() {
char *v = read_bi("122323232323232323232456789");
printf("The return value is: %s\n", v);
char *v1 = read_bj("3242656456745");
printf("The return value is: %s\n", v1);
char *my_value_t[] = {
"00123",
"00234",
"00345"
};
for (int i = 0; i < sizeof(my_value_t) / sizeof(my_value_t[0]); i++) {
printf("Counting: %d\n", i);
printf("The text found is: %s\n", my_value_t[i]);
}
}
char *read_bi(char *value) {
printf("[TEST-1] The entered value of \"value\" is: %s\"\n", value);
return value;
}
char *read_bj(char value[10]) {
printf("[TEST-2] The entered value of \"value\" is: %s\"\n", value);
return value;
}
void bprint(char *template, ...) {
char timestr[19] = {0};
time_t t = time(NULL);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&t));
va_list list;
va_start (list, template);
char *chptr = template;
while( chptr != NULL) {
fprintf(stderr, "%s - %s \n", timestr, chptr);
// vfprintf(stderr, template, list);
chptr = va_arg(list, char *);
}
va_end (list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment