Thoughts Of The Day

COLOURS IN TEXT MODE

        Going forward in Advance Learning Tutorial of C/C++, today we will learn about the colors in C/C++. Yet now you will have been using only two colors "Black and White" in your program. Black color for Background and White color for foreground to print the characters on the screen.

       By default all compilers uses of these two colors. But if you like to print any of the characters on the screen by your favorite colors so this tutorial can help you.

Important functions are explained below. 
      
     
Note that Functions described below will workd only on Text Mode.With the help of colors you can make your program even more attractive. 

       In C/C++ total 15 colors are defined. To use any of them, simply you can use name of the color or you can also use the corresponding value of that color. This value has been already defined in the compiler. 

 
     15 colors and their values
​given ​in the table below.
   
Colors Values
1. BLACK 0
2. BLUE 1
3. GREEN 2
4. CYAN 3
5. RED 4
6. MAGENTA 5
7. BROWN 6
8. LIGHTGRAY 7
9. DARKGRAY 8
10. LIGHTBLUE 9
11. LIGHTGREEN 10
12. LIGHTCYAN 11
13. LIGHTRED 12
14. LIGHTMAGENTA 13
15. YELLOW 14
16. WHITE 15


 Before using colors make sure to include an important Header File <conio.h> in your program.

      In Text Mode there are two Basic functions defined in C/C++ for use colors in your program. Please note that both the functions are declared in the <conio.h> Header file, that's why you have to include this file in your program otherwise error will occur in your program. Now we are going to learn how to use these functions and make a simple program using these functions.


Functions are given below.

         1. textcolor (int color)

         2. textbackground (int color)

        textcolor (int color):- This function sets the color of the character in Text Mode. Just pass the name of the color or corresponding value (which is shown in the above table) as the value in parameter of the function, in which color you would like to display characters on screen.
  
        textbackground (int color):- This function sets the background color of the character in Text Mode. This function will also use as the same way, Pass the name of the color or corresponding value of that color in parameter of the function. See example below.


textcolor( RED );
textbackgrond( YELLOW ); 
        
   or 

textcolor( 4 );
textbackgrond( 14 );     

 

      

 You can use any method in your program. Both ways are wright. Both of this will give the same output. 

         An example of the program is given below. You can see the program and try to run on your computer.



#include <conio.h> 

void main() {

clrscr();

textcolor(YELLOW);

cprintf("Welcome In Advance Learning Tutorial\n");

textcolor(RED);

cprintf("Hello\n");

getch(); 
}





 In the above program note that here "cprintf" function is used instead of "printf". 
  
          If you will use "printf" function then the color of output character will have no effect and it is uses by default White color.

         'cprintf' is a console output function means that this function work for functions that produce text mode output directly to the screen (console output functions).  

         That's enough for today's Advance Learning Tutorial of C/C++. In the Topics mentioned above if you have any doubt or any problem so you can ask here. We will definitely resolve your problems as soon as possible.

         Now in next chapter of C/C++ you will learn about some of the interesting functions (delay and gotoxy) which are essential for graphics programming. 

        If you like this blog, share with your friends, because this might also be useful for them! Through the link you can share it easily on Facebook, Twitter, linked In or Google+.



                           

  

Back ChapterIndex Page
              
                           
                               Read This Blog In Hindi Click here                             

No comments:

Post a Comment


For MNCs Interview Preparation And Practice set of C,C++,JAVA,PHP Questions Like our page.