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 instance, 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.
 #include <stdio.h> #define PERIOD `.' int main(void) { int ch; int charcount = 0; while ((ch = getchar()) != PERIOD) { if (ch != `"' && ch != `\'') charcount++; } printf("There are %d non-quote characters.\n", ...

Get C Primer Plus®, Third 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.