by Dinesh Thakur Category: Control Structures

The program segment given below accepts a character from the keyboard and prints the name of the corresponding color, e. g., if the user enters character R, it prints Red. However, it handles only three colors, namely, red, green and blue.

The character entered from the keyboard is first accepted in variable color (of type char) and is then tested in the switch statement. The program includes three cases corresponding to the colors red, green and blue. Note that the case values are character constants 'R’ ,‘G' and 'B' . Each case includes a printf statement to print the name of the color followed by a break statement. The output of program containing the above code is shown below.

#include <stdio.h>

void main()


     char color;


     printf("Enter character (R/G/B): ");

             color= getchar();

             switch (color)


                       case 'R':

                               printf ("Red") ;


                       case 'G':



                       case 'B':






    C Program for print a name of color that start with a given character

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles