r/cprogramming • u/Historical-Chard-248 • Jan 05 '26
Progress Bar
Hi, I'm a beginner and I want to make a progress bar like this:
[###########] 100%
Where the text and percentage update every second, it's more of a simulation of a progress bar than a real one. Does anyone know how to do this?
14
Upvotes
27
u/JaguarMammoth6231 Jan 05 '26
Just a couple hints...printing a carriage return "\r" will move the cursor back to the beginning of the line. So just put that at the beginning of your print and don't put a newline at the end. You may need to use fflush(stdout) too.
5
u/This_Growth2898 Jan 05 '26
C standard output functions are designed to work with something like teletype (a typewriter connected to the network cable); it can't go backward efficiently.
It is console-dependent, so you need to provide us with your OS and console to get additional advice; in most cases, Afraid-Locksmith6566 is right, and you can use the '\b' symbol (backspace) to do this, but often you can do better.
5
u/F1nnyF6 Jan 05 '26
This is what you are after. It is my Bible for DIY terminal graphics
2
u/tyerofknots Jan 05 '26
What I have done previously is something like:
Then I use ANSI escape codes to clear the screen and position the cursor and such. If you need help with the ANSI escape codes, there's a resource on GitHub!
2
u/amanuense Jan 05 '26
Do you want to make it. Or do you want the functionality?
The difference will tell us how to better help you. If you need the functionality there are libraries such as ncurses and other simpler ready to use implementations you can get from github
2
u/Afraid-Locksmith6566 Jan 05 '26
Console specific codes and or \b
1
u/Fit-Relative-786 Jan 06 '26
In a vt100 terminal use the string before writing the output you want.
“\33[2k\r” then flush the output.
Don’t add a newline.
printf(“\33[2k\r%s”, progress_string); fflush(stdout);
1
u/Alive-Bid9086 Jan 06 '26
I would look into the ncurses or curses library
1
u/Specialist-Cicada121 Jan 06 '26
I shared something similar a few weeks back, if you're interested: https://www.reddit.com/r/C_Programming/s/nUTyUJSUUs
1
u/jwzumwalt Jan 07 '26
You may find some value in this programmers "progress bar" written for Bash.
https://www.youtube.com/watch?v=r2rbAvXMcXQ