test Blog
Happy living

printf 输出特效

老鼠 posted @ 2009年3月11日 02:38 in 编程相关 with tags printf 输出 , 3994 阅读

给printf的输出加上些特效比如颜色,可以让打印信息更鲜明,在debug的时候特别有用。

颜色:


 

  1. #define NONE         "\033[m"
  2. #define RED          "\033[0;32;31m"
  3. #define LIGHT_RED    "\033[1;31m"
  4. #define GREEN        "\033[0;32;32m"
  5. #define LIGHT_GREEN  "\033[1;32m"
  6. #define BLUE         "\033[0;32;34m"
  7. #define LIGHT_BLUE   "\033[1;34m"
  8. #define DARY_GRAY    "\033[1;30m"
  9. #define CYAN         "\033[0;36m"
  10. #define LIGHT_CYAN   "\033[1;36m"
  11. #define PURPLE       "\033[0;35m"
  12. #define LIGHT_PURPLE "\033[1;35m"
  13. #define BROWN        "\033[0;33m"
  14. #define YELLOW       "\033[1;33m"
  15. #define LIGHT_GRAY   "\033[0;37m"
  16. #define WHITE        "\033[1;37m"

比如:


 

  1. printf("\033[31m ####----->> \033[32m" "hello\n" "\033[m");

颜色分为背景色和字体色,30~39用来设置字体色,40~49设置背景:

        背景色                        字体色
        40: 黑                          30: 黑
        41: 红                          31: 红
        42: 绿                          32: 绿
        43: 黄                          33: 黄
        44: 蓝                          34: 蓝
        45: 紫                          35: 紫
        46: 深绿                      36: 深绿
        47: 白色                      37: 白色

记得在打印完之后,把颜色恢复成NONE,不然再后面的打印都会跟着变色。

 

另外,还可以加一些ANSI控制码。加颜色只是以下控制码中的一种:

\033[0m   关闭所有属性  
\033[1m   设置高亮度  
\033[4m   下划线  
\033[5m   闪烁  
\033[7m   反显  
\033[8m   消隐  
\033[30m   --   \033[37m   设置前景色  
\033[40m   --   \033[47m   设置背景色  
\033[nA   光标上移n行  
\033[nB   光标下移n行  
\033[nC   光标右移n行  
\033[nD   光标左移n行  
\033[y;xH设置光标位置  
\033[2J   清屏  
\033[K   清除从光标到行尾的内容  
\033[s   保存光标位置  
\033[u   恢复光标位置  
\033[?25l   隐藏光标  
\033[?25h   显示光标

Avatar_small
xiaoee 说:
2009年3月12日 01:23

怎么弄的,我怎么搞不出来啊?

Avatar_small
孤毒老鼠 说:
2009年3月12日 01:34

@xiaoee: 就是在printf里面加啊,上面不是有演示了吗?
printf("\033[31m ####----->> \033[32m" "hello\n" "\033[m");
比如这个~~就用了2个颜色,最后恢复为正常颜色~~
用了\033[31m 这段字后面的字符就是红色的
\033[32m这段后面的hello就是绿色的~~~
最后那个是恢复正常颜色,不恢复的话会一直是绿色,呵呵

192.168.0.1 说:
2023年7月15日 23:01

192.168.0.1 est une adresse réseau IPV4 privée. La console de gestion du réseau est accessible via un navigateur en recherchant dans la barre de recherche, cela peut être utilisé chaque fois qu’un utilisateur rencontre un problème, le système peut être un ordinateur ou un téléphone. 192.168.0.1 Une passerelle par défaut peut être configurée à l’aide d’un routeur à usage domestique, en parcourant 192.168.0.1 , l’utilisateur peut résoudre tous les problèmes, c’est le seul équipement qui peut résoudre tous les problèmes connexes sans créer de conflits d’adresse.

Assam 6th Class Que 说:
2023年7月16日 16:44

Good News Assam Students who are Searching for the Assam 6th Model Paper 2024 Assam Board has been Uploaded the All Subject Wise Download to the Students who are Going to Appear in the Board Examination this year Assam 6th Class Question Paper 2024 SEBA has Released the Assam 6th Model Paper 2024 in PdF Format for All Hindi, English, Sanskrit, Science, Mathematics Subjects this web page Available here for Download, Assam Students Studying in 6th Class have to Participate in Examination for Better Preparation.Students who have completed their Preparation can Start Practicing the SEBA 6th Class Model Question Paper 2024. we Suggest the Students Solved Question Paper for Practice Purpose


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter