Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Created March 15, 2014 05:32
Show Gist options
  • Save jweyrich/9562260 to your computer and use it in GitHub Desktop.
Save jweyrich/9562260 to your computer and use it in GitHub Desktop.
quick & simple printing with indentation level (spaces, not tabs)
#include <stdio.h>
#include <stdarg.h>
void outputf(int level, const char *format, ...) {
FILE *stream = stdout;
static const int tab_size = 4;
const int width = level * tab_size + 1;
va_list args;
va_start(args, format);
if (level > 0) {
fprintf(stream, "%*s", width, " ");
}
vfprintf(stream, format, args);
va_end(args);
}
void output(int level, const char *str) {
outputf(level, "%s", str);
}
int main() {
output(0, "no indentation\n");
for (int i=0; i < 10; i++) {
outputf(i, "indented by %d\n", i);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment