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 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 color’s name followed by a break statement. The output of the program containing the above code shown below.
#include<stdio.h> void main() { char color; clrscr(); printf("Enter character (R/G/B): "); color= getchar(); switch (color) { case 'R': printf ("Red") ; break; case 'G': printf("Green"); break; case 'B': printf("Blue"); break; } getch(); }