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

An Initial Example

You are already somewhat familiar with the while loop, but let's review it with a program that sums integers entered from the keyboard (see Listing 6.1). This example makes use of the return value of scanf() to terminate input.

Listing 6.1. The summing.c Program
 /* summing.c -- sums integers entered interactively */ #include <stdio.h> int main(void) { long num; long sum = 0L; /* initialize sum to zero */ int status; printf("Please enter an integer to be summed. "); printf("Enter q to quit.\n"); status = scanf("%ld", &num); while (status == 1) /* == means "is equal to" */ { sum = sum + num; printf("Please enter next integer to be summed. "); printf("Enter q to quit.\n"); status = scanf("%ld", &num); } printf("Those integers ...

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