Adding Color to Your Output From C – The Urban Penguin

Share on facebook
Share on google
Share on twitter
Share on linkedin
Adding Color to Your Output From C – The Urban Penguin

Including Shade to Your Packages

Adding ColorIncluding colour to the output of your applications could make them extra visually interesting and including to their aesthetics. Not solely this, although, utilizing colour will make notices or warning extra noticeable to your customers leading to higher acceptance. On this weblog we’ll see how you can can each add colour and begin making use of extra features in C, quite than, utilizing soley the primary operate. So if you find yourself prepared and sat comfortably in your chair we will start.

ANSI Colours

Now we have 8 ANSI colours that we will use in our output, this may be doubled to 16 should you think about that these colours might be displayed as commonplace or in daring for highlighting. To have the ability to entry the colours we have to use and escape sequence adopted by the right colour code, the print the textual content and at last reset the colours.

You are watching: c printf change color

  • printf(“33[0;31m”); //Set the textual content to the colour purple
  • printf(“Hellon”); //Show Hey in purple
  • printf(“33[0m”); //Resets the textual content to default colour
  • Escape is: 33
  • Shade code is: [0;31m

You will need to reset the colour to making sure that the chosen colour is terminated and textual content returns to regular. Utilizing the next desk you’ll be able to view a few of the code obtainable.

Easy Hey World in Shade

Working with a easy howdy world program we will start to grasp how you can make use of the colour. Firstly we’ll set the colour to be purple and daring earlier than transferring onto utilizing features to set the colour.

Refer: f.lux: software to make your life better | Photoshop Services

#embrace <stdio.h> int important () { printf(“33[1;31m”); printf(“Hey worldn”); printf(“33[0m”) return 0; }

For BOLD simply exchange the preliminary 0 earlier than the ; with a 1. BOLD inexperienced would be33[1;32m

  • Black 33[0;30m
  • Purple 33[0;31m
  • Inexperienced 33[0;32m
  • Yellow 33[0;33m
  • Blue 33[0;34m
  • Purple 33[0;35m
  • Cyan 33[0;36m
  • White 33[0;37m

Including colour to the output was actually fairly easy; nevertheless setting many colours or altering the colours many instances can be repetitive. Setting the colour typically and utilizing multiple colour goes to be required the place we need to spotlight the output with data and warnings.

Utilizing Capabilities

That is the place features will help. It’s easy to create features for purple, yellow and many others. Let’s have a look, assuming we create the supply file helloc.c.

embrace <stdio.h> void purple () { printf(“33[1;31m”); } void yellow { printf(“33[1;33m”); } void reset () { printf(“33[0m”); } int important () { purple(); printf(“Hey “); yellow(); printf(“worldn”); reset(); return 0; }$ gcc -Wall -o helloc helloc.c $ ./helloc Hey World!

Read more: how to make your id photo look good | Photoshop Services

We are able to use the newly created features as many time as we would like and it is so simple as yellow(); reset(); or purple(); We, in fact, can create extra features to assist all colours.

Utilizing the features the best way now we have we don’t have to return any values. The features end result solely in printing the ANSI colour codes to the terminal. As we don’t return any worth then we set the output parameter explicitly to void.

Shifting On

It’s also seemingly that we will reuse these operate in nearly any C program that has textual content output. In one other weblog, we’ll see how we will reuse this code by creating and referencing our personal header information.

The next video steps you thru the method of printing in colour.

Danh mục: How to?
Nguồn: https://photoshopservices.net

Leave a Reply

×

Powered by WhatsApp Chat

× How can I help you?
%d bloggers like this: