The if Statement

Let's start with a simple example of an if statement shown in Listing 7.1. This program reads in a list of daily low temperatures (in Celsius) and reports the total number of entries and the percentage that were below freezing. It uses scanf() in a loop to read in the values. Once during each loop cycle, it increments a counter to keep track of the number of entries. An if statement detects temperatures below freezing and keeps track of their number separately.

Listing 7.1 The colddays.c program.
 /* colddays.c -- finds percentage of days below freezing */ #include <stdio.h> #define SCALE "Celsius" #define FREEZING 0 int main(void) { float temperature; int cold_days = 0; int all_days = 0; printf("Enter the list of daily low ...

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.