Solved Examples · Example 33
Q.Write a C program to accept the sex code of a person and display whether the person is a male or a female.
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include <stdio.h>
void main()
{
char, sex-code;
printf("Enter the sex code(m/f)\n");
scanf("%c", &sex_code);
switch (sex_code)
{
case'm': printf("MALE\n");
break;
case'f': printf("FEMALE\n");
break;
default: printf("Error in typing sexcode\n");
break;
}
/* End of switch */
} /*End of main()*/
Sample output printed in the textbook:
Enter the sex code (m/f)
f
FEMALE
#include <stdio.h>
void main()
{
char, sex-code;
printf("Enter the sex code(m/f)\n");
scanf("%c", &sex_code);
switch (sex_code)
{
case'm': printf("MALE\n");
break;
case'f': printf("FEMALE\n");
break;
default: printf("Error in typing sexcode\n");
break;
}
/* End of switch */
} /*End of main()*/
Enter the sex code (m/f)
f
FEMALE
Karnataka PUCTextbookLongImportance★★★★★est
94% · 145/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
A switch on the sex-code character prints MALE ('m') or FEMALE ('f'), else an error; 'f' -> FEMALE.
This example highlights the role of break in a switch: after the matching case runs, break exits the switch so control does not fall through into the following cases. Character constants such as 'm' and 'f' select the branch, and the default label reports an invalid code. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.