Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Created July 5, 2013 00:11
Show Gist options
  • Save EdgeCaseBerg/5930906 to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/5930906 to your computer and use it in GitHub Desktop.
Using the \b escape character in C for a simple counter
#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