O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 the number of such days separately.

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required