ctexttext-formattingansi-escapebold

How to bold text in c program


How do I Bold my PrintF? .. ( I am new in C)

#include <stdio.h>

int main() {
    int i;
    for (i=1; i<=5; i++) {
        printf("Md.Mehedi hasan");
    }
    return 0;
}

Solution

  • Tested in the terminal in Linux Ubuntu 18.04.

    To make that a little more readable, use some string literal macros for the color and formatting codes, like this:

    #include <stdio.h>
    
    #define COLOR_BOLD  "\e[1m"
    #define COLOR_OFF   "\e[m"
    
    int main(void)
    {
        for (int i = 1; i <= 5; i++)
        {
            printf(COLOR_BOLD "Md.Mehedi hasan\n" COLOR_OFF);
        }
    
        return 0;
    }
    

    Build and run cmd:

    mkdir -p bin && \
    gcc -Wall -Wextra -Werror -O3 -std=c17 printf_bold_and_colors.c -o bin/a && \
    bin/a
    

    Here are a bunch more examples from some bash code of mine: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-diffn.sh#L126-L138:

    # ANSI Color Code Examples to help make sense of the regex expressions below
    # Git config color code descriptions; see here:
    # https://stackoverflow.com/questions/26941144/how-do-you-customize-the-color-of-the-diff-header-in-git-diff/61993060#61993060
    # ---------------    ----------------------------------------------------------------
    #                    Git config color code desription
    # ANSI Color Code    Order: text_color(x1) background_color(x1) attributes(0 or more)
    # ----------------   ----------------------------------------------------------------
    # \033[m             # code to turn off or "end" the previous color code
    # \033[1m            # "white"
    # \033[31m           # "red"
    # \033[32m           # "green"
    # \033[33m           # "yellow"
    # \033[34m           # "blue"
    # \033[36m           # "cyan"
    # \033[1;33m         # "yellow bold"
    # \033[1;36m         # "cyan bold"
    # \033[3;30;42m      # "black green italic" = black text with green background, italic text
    # \033[9;30;41m      # "black red strike" = black text with red background, strikethrough line through the text
    

    You can replace \033 (octal 33) with \e, as they mean the same thing just in different representations. See: https://en.wikipedia.org/wiki/ASCII#Control_code_chart.

    For a bunch more color and formatting number codes, see this table here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

    You can use multiple formatting numbers by separating them with a semicolon (;), as shown in the examples below.

    A couple more really cool examples in C:

    // 1 = bold; 5 = slow blink; 31 = foreground color red
    // 34 = foreground color blue
    // See: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
    #define COLOR_BOLD_SLOW_BLINKING      "\e[1;5m"
    #define COLOR_BOLD_SLOW_BLINKING_RED  "\e[1;5;31m"
    #define COLOR_BOLD_BLUE               "\e[1;34m"
    
    // Make "hello" bold and slow blinking and red, and "world" just bold and blue
    printf(COLOR_BOLD_SLOW_BLINKING_RED "hello " COLOR_OFF 
           COLOR_BOLD_BLUE "world\n" COLOR_OFF);
    

    enter image description here

    Going further

    1. Run some more ANSI color code text colorization and formatting examples with blinking text and alternating even/odd color patterns in my eRCaGuy_hello_world repo here: printf_bold_and_colors.c

    References

    1. The answer by SGeorgiades
    2. Gif tools (how I made the gif animated image above):
      1. Screen recorded with OBS studio; follow this tutorial for partial screen capture: https://www.youtube.com/watch?v=ypMDvZ_Jgl4
      2. .mkv video converted to .gif with ffmpeg -i in.mp4 out.gif, per my comment here.