如何在c程序中将文本加粗

发布于 2025-01-10 07:02:55 字数 195 浏览 3 评论 0原文

如何将 PrintF 加粗? ..(我是C语言新手)

#include <stdio.h>

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

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2025-01-17 07:02:55

在 Linux Ubuntu 18.04 的终端中进行了测试。

为了使其更具可读性,请使用一些字符串文字宏来表示颜色和格式代码,如下所示:

#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;
}

构建并运行 cmd:

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

这里有更多示例来自我的一些 bash 代码: 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

您可以替换 \033 (八进制 < code>33) 与 \e,因为它们的含义相同,只是表示形式不同。请参阅:https://en.wikipedia.org/wiki/ASCII#Control_code_chart

有关更多颜色和格式数字代码,请参阅此处的表:https:/ /en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

您可以通过使用分隔符来使用多个格式数字分号 (;),如下面的示例所示。

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);

在此处输入图像描述

进一步

  1. 在我的 < 中运行一些带有闪烁文本和交替偶数/奇数颜色模式的 ANSI 颜色代码文本着色和格式化示例一个href="https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world" rel="nofollow noreferrer">eRCaGuy_hello_world 存储库:printf_bold_and_colors.c

参考文献

  1. SGeorgiades 的答案
  2. Gif 工具(如何我制作了上面的 gif 动画图像):
    1. 使用OBS studio录制的屏幕;请按照本教程进行部分屏幕截图:https://www.youtube.com/watch?v= ypMDvZ_Jgl4
    2. .mkv 视频使用 ffmpeg -i in.mp4 out.gif 转换为 .gif,根据我的评论

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.
后来的我们 2025-01-17 07:02:55

如果您的终端支持 ANSI 转义序列,您可以执行以下操作:

#include <stdio.h>

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

\e 是 ESC 字符(ASCII 27 或 0x1B),ESC [ 1 m 设置粗体,并且ESC [ m 重置显示属性,即重置粗体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文