3

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;
}
3
  • 3
    There's no standard way of doing that. People usually use a library like curses or add raw escape sequences into their programs.
    – Ted Lyngmo
    Commented Feb 26, 2022 at 5:15
  • please use proper indentation even in short programs
    – jsotola
    Commented Feb 26, 2022 at 5:17
  • @Md. Mehedi hasan, don't forget to upvote any question or answer you find helpful. The fact that you marked an answer here as correct means you should definitely upvote it as well. It motivates answerers to keep answering questions when you show appreciation for helpful answers with an upvote. Commented Mar 5, 2022 at 18:09

2 Answers 2

4

If your terminal supports ANSI Escape Sequences, you can do this:

#include <stdio.h>

int main(void)
{
    for(int i = 1; i <= 5; i++)
        printf("\e[1mMd.Mehedi hasan\e[m");
    return 0;
}

The \e is the ESC character (ASCII 27 or 0x1B), and ESC [ 1 m sets bold, and ESC [ m resets the display attributes, which resets bold.

3

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.
2
  • 1
    wow thanks!!! You exactly gave me the answer i need!!!! Commented Mar 7, 2022 at 6:37
  • @Md.Mehedihasan, hey, glad to help! I appreciate you marking my answer as "correct". Don't forget to also upvote this and any other answer you find useful. That provides motivation for us answerers to keep on answering, and it helps others quickly see which answers people found most useful. Commented Mar 7, 2022 at 6:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.