Let's Get Logical
You've seen how if and while statements often use relational expressions as tests. Sometimes you will find it useful to combine two or more relational expressions. For example, suppose you want a program that counts how many times the characters other than single or double quotes appear in an input sentence. You can use logical operators to meet this need, and you can use the period character (.) to identify the end of a sentence. Listing 7.6 presents a short program illustrating the method.
Listing 7.6. The chcount.c Program
// chcount.c -- use the logical AND operator #include <stdio.h> #define PERIOD '.' int main(void) { int ch; int charcount = 0; while ((ch = getchar()) != PERIOD) { if (ch != '"' && ch != '\'') charcount++; ... |
Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.