CodeBlanks Logo Icon CodeBlanks

printf-like function

  • C course
  • Intermediate
  • Lesson 78/116
  • 5 min

Learn to write your own printf-like function.

printf-like functions with vprintf

If you want to write your own function that behaves like printf, you use vprintf. This function takes a va_list and prints according to the format string.

Signature: void log_msg(const char *level, const char *fmt, ...). Inside, first print the level (printf("%s: ", level)), then va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); to print the formatted part. vprintf is the va_list-taking variant of printf; it uses the same format specifiers (%d, %s, etc.). This pattern is the basis of logging functions. vfprintf and vsprintf similarly write to a file and to a string.

The log_msg example

A real log_msg function combines a level and a formatted message. The output might be INFO: Sum 42.

Definition: void log_msg(const char *level, const char *fmt, ...) { printf("%s: ", level); va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); printf("\n"); }. First the level and colon are printed, then the formatted part with vprintf, finally a newline. The second argument to va_start is fmt because that's the last fixed parameter. Call: log_msg("INFO", "Sum %d", 42). This pattern is the basis of libraries that log at different levels; you can change the level and reuse the same format.

Practice The log_msg function

Write void log_msg(const char *level, const char *fmt, ...). Start with printf("%s: ", level);. Call va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args);. Finally printf("\n");. In main call log_msg("INFO", "Sum %d", 42);.

Starter code

main.c
#include <stdio.h>
#include <stdarg.h>

// log_msg: print a message with level and format
// print level with printf; then va_list, va_start, vprintf, va_end, newline
// main: call log_msg("INFO", "Sum %d", 42)

int main(void) {
    // write here
    return 0;
}

Sample solution

main.c
#include <stdio.h>
#include <stdarg.h>

void log_msg(const char *level, const char *fmt, ...) {
    printf("%s: ", level);
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
    printf("\n");
}

int main(void) {
    log_msg("INFO", "Sum %d", 42);
    return 0;
}

Expected output

INFO: Sum 42

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 The vprintf family

Term Meaning
vprintf print to screen
vfprintf print to file
vsprintf print to string
vsnprintf print to bounded string

2 The vprintf call

Question
vprintf(fmt, ____);
Answer
vprintf(fmt, args);

Explanation: vprintf(fmt, args) produces the formatted output.

3 What's the difference between vprintf and printf?

  • vprintf is slower
  • printf takes a va_list
  • vprintf takes a va_list Correct answer
  • vprintf takes no format

Explanation: vprintf is the va_list-taking variant of printf.

All lessons in Intermediate