Fundamentals 6 min read

How to Build a Real-Time Console Progress Bar in C

This tutorial explains how to create a console progress bar in C by covering newline versus carriage return handling, output buffering strategies, using fflush to force screen updates, and adapting the code for multi‑digit countdowns.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Real-Time Console Progress Bar in C

In routine server maintenance we often need a visual progress indicator; this article shows how to implement such a progress bar in C, covering newline vs carriage return, output buffering strategies, using fflush to force screen updates, and handling multi‑digit countdowns.

1. Carriage Return and Newline

Newline moves to the next line (\n), while carriage return returns to the start of the current line (\r). In C, \n typically means newline plus return to the line start.

2. Buffering

Two code examples illustrate how output buffering affects when text appears on the screen.

#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("I am a proc
"); // contains 

    sleep(3);
    return 0;
}

This prints immediately, then sleeps for 3 seconds.

#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("I am a proc"); // no 

    sleep(3);
    return 0;
}

Here the output is buffered and not shown until the program ends.

The three buffering strategies are:

Unbuffered: data is written directly to the device.

Line buffered: data is flushed when a newline is encountered.

Fully buffered: data is flushed only when the buffer is full.

The first example uses line buffering, so the newline forces a flush.

3. Countdown Program

Printing a number followed by a carriage return can create a countdown effect, but without a newline the output stays in the buffer.

#include <stdio.h>
#include <unistd.h>

int main()
{
    int count = 3;
    while(count >= 0)
    {
        printf("%d\r", count--);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

Using fflush(stdout) forces each number to appear immediately.

For two‑digit countdowns, the carriage return always returns to the first character, so the second digit does not update unless we format the output width.

#include <stdio.h>
#include <unistd.h>

int main()
{
    int count = 10;
    while(count >= 0)
    {
        printf("%2d\r", count--); // width 2
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

4. Progress Bar Program

#include <stdio.h>
#include <string.h>
#include <unistd.h>

void ProcBar()
{
    int i = 0;
    char proc[102];
    memset(proc, '\0', sizeof(proc));

    while(i <= 100)
    {
        // left‑align with '-'
        printf("[%-100s] [%d%%]\r", proc, i);
        fflush(stdout);
        proc[i] = '#';
        usleep(100000); // 0.1 second
        i++;
    }
    printf("
");
}

int main()
{
    ProcBar();
    return 0;
}

This function builds a visual progress bar that updates in place, showing the percentage completed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cprogress barconsoleCountdownfflushOutput Buffering
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.