Created
July 5, 2013 00:11
-
-
Save EdgeCaseBerg/5930906 to your computer and use it in GitHub Desktop.
Using the \b escape character in C for a simple counter
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> | |
//Compile with: $cc counter.c | |
//run with ./a.out | |
main(){ | |
int i; | |
for (i = 0; i < 10; ++i){ | |
//The \b is a lesser known escape character that moves the cursor back one. Allowing you to overwrite what you've previously written. | |
printf("\b%i",i); | |
//printf buffers it's output, so when the sleep timer hits, the input is still in the buffer. | |
//You need to flush stdout in order to see the change each iteration otherwise you'll just see 9 on execution | |
fflush(stdout); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment