C Boolean Examples: Real-Life Applications
This page explores practical examples of using Boolean expressions in C programming. It demonstrates how Boolean logic can be applied to real-world scenarios, such as determining voting eligibility and checking if a number is even or odd.
C Boolean Examples
Real Life Example: Checking Voting Eligibility
Let's consider a "real life example" where we need to determine if a person is old enough to vote. In the example below, we use the >=
comparison operator to check if a person's age (in this case, 25) is greater than or equal to the voting age, which is set at 18.
Example
int myAge = 25;
int votingAge = 18;
printf("%d", myAge >= votingAge); // Returns 1 (true), meaning 25 year olds are allowed to vote!
Output
1
Improving with an If...Else Statement
A more refined approach would be to wrap the code in an if...else
statement. This allows us to perform different actions based on the result, such as displaying a message depending on whether the person is old enough to vote.
Example
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
printf("Old enough to vote!");
} else {
printf("Not old enough to vote.");
}
Output
Old enough to vote!