You can't with normal functions. But some functions take va_list parameters, and that lets you do it. For example, you can use vfprintf() like so.
Code:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
void log_message(const char *format, ...);
int main() {
log_message("Starting\n");
printf("Press any key.\n");
getchar();
log_message("Ending, elapsed time = %d\n", (int)clock());
return 0;
}
void log_message(const char *format, ...) {
time_t t;
const char *time_string;
va_list args;
va_start(args, format);
time(&t);
time_string = ctime(&t);
printf("[%.*s] ", strlen(time_string) - 1, time_string);
vfprintf(stdout, format, args);
va_end(args);
}
Input/output:
Code:
[Thu Sep 10 16:40:48 2009] Starting
Press any key.
[Thu Sep 10 16:40:50 2009] Ending, elapsed time = 0
[edit] Quzah types too fast. Or, more likely, I type too much. [/edit]