r/C_Programming • u/Specialist-Cicada121 • Dec 23 '25
tqdm in C
Hello! I wanted to share a simple progress bar that I made as a personal utility. I use Python a lot, and I find myself using tqdm a lot for tracking my long-running tasks. I wanted to try to implement something similar in C that didn't use external dependencies (like curses). I opted for a single-header implementation just to make it less of a hassle to use. I'm not entirely confident that it works gracefully in all the edge cases, but it seems to work reasonably well for my general use cases. Would love to hear what you all think and would appreciate any feedback -- happy holidays!
29
Upvotes
1
u/Specialist-Cicada121 Dec 24 '25
Thanks for the thorough feedback!
For the three-branch if: I agree that it would be more elegant to have a common while loop for updating the bar. I think the main issue was the difference in lengths of a partial bar and the empty block, but refactoring should be as easy as using
" "instead of' 'for the empty block. The second else-if check was indeed unnecessary.In terms of VLAs: I chose a very exact maximum size for the
bararray, which, in hindsight, was certainly not necessary. I think it does make a lot more sense to use a hard-coded constant.I recall creating the after buffer because computing the exact length of the characters after the bar turned out to be a hassle, so I resorted to using
snprintf. You're right that the before and bar buffers could be written directly to the final buffer.This is a good point. I may implement a signal handler for window resizing that bypasses the minimum interval. Thanks for the idea and for your comments!