• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / C Programming / Adding Color to Your Output From C

Adding Color to Your Output From C

September 17, 2016 by The Urban Penguin

Adding Color to Your Programs

Adding ColorAdding color to the output of your programs can make them more visually appealing and adding to their aesthetics. Not only this, though, using color will make notices or warning more noticeable to your users resulting in better acceptance. In this blog we will see how to can both add color and start making use of additional functions in C, rather than, using soley the main function. So when you are ready and sat comfortably in your chair we shall begin.

ANSI Colors

We have 8 ANSI colors that we can use in our output, this can be doubled to 16 if you take into consideration that these colors can be displayed as standard or in bold for highlighting. To be able to access the colors we need to use and escape sequence followed by the correct color code, the print the text and finally reset the colors.

  • printf(“\033[0;31m”); //Set the text to the color red
  • printf(“Hello\n”); //Display Hello in red
  • printf(“\033[0m”); //Resets the text to default color
  • Escape is: \033
  • Color code is: [0;31m

It is important to reset the color to ensuring that the selected color is terminated and text returns to normal. Using the following table you can view some of the code available.

Simple Hello World in Color

Working with a simple hello world program we can begin to understand how to make use of the color. Firstly we will set the color to be red and bold before moving onto using functions to set the color.

#include <stdio.h> int main () { printf(“\033[1;31m”); printf(“Hello world\n”); printf(“\033[0m”) return 0; }

For BOLD just replace the initial 0 before the ; with a 1. BOLD green would be\033[1;32m

  • Black \033[0;30m
  • Red \033[0;31m
  • Green \033[0;32m
  • Yellow \033[0;33m
  • Blue \033[0;34m
  • Purple \033[0;35m
  • Cyan \033[0;36m
  • White \033[0;37m

Adding color to the output was really quite simple; however setting many colors or changing the colors many times will be repetitive. Setting the color often and using more than one color is going to be required where we want to highlight the output with information and warnings.

Using Functions

This is where functions can help. It is simple to create functions for red, yellow etc. Let’s take a look, assuming we create the source file helloc.c.

#include <stdio.h>
void red () {
  printf("\033[1;31m");
}
void yellow {
  printf("\033[1;33m");
}
void reset () {
  printf("\033[0m");
}
int main () {
  red();
  printf("Hello ");
  yellow();
  printf("world\n");
  reset();
  return 0;
}
$ gcc -Wall -o helloc helloc.c
$ ./helloc 
Hello World!

We can use the newly created functions as many time as we want and it is as simple as yellow(); reset(); or red(); We, of course, can create more functions to support all colors.

Using the functions the way we have we do not need to return any values. The functions result only in printing the ANSI color codes to the terminal. As we do not return any value then we set the output parameter explicitly to void.

Moving On

It is also likely that we can reuse these function in almost any C program that has text output. In another blog, we will see how we can reuse this code by creating and referencing our own header files.

The following video steps you through the process of printing in color.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: C Programming

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Linux Technologies
    • Apache HTTPD Server
    • Learning PHP
    • Learning PUPPET
    • Learning SAMBA
    • Linux File-Systems
    • Monitoring with Nagios Core
    • MYSQL
    • openLDAP Directories on Linux
  • LPI Training from The Urban Penguin
    • Complete Linux Essentials
    • Live and Pluralsight hosted courses
    • LPI Linux Essentials
    • LPI Linux Essentials for Raspberry Pi
    • LPIC-1 Linux Administrator
    • LPIC-2 Certified Linux Engineer
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
  • Online Instructor-led Courses
    • Bash Scripting Masterclass
    • Nftables Masterclass
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
  • OpenStack
    • Citrix Videos
    • Pluralsight
    • Raspberry Pi Tutorials
    • Udemy
  • Operating System Tutorials
    • Learning SUSE
    • Learning Ubuntu
    • Linux Foundation Training
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
    • Solaris 11 OCA 1ZO-821
  • Scripting – the power of repetition!
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
    • Learn C Programming using Linux and the Raspberry Pi
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell

© 2022 The Urban Penguin · All Rights Reserved