Created
March 15, 2014 05:32
-
-
Save jweyrich/9562260 to your computer and use it in GitHub Desktop.
quick & simple printing with indentation level (spaces, not tabs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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