Introductory Program

By now, you probably expect a sample program at the beginning of each chapter, so Listing 4.1 is a program that engages in a dialog with the user. To add a little variety, the code uses the new C99 comment style.

Listing 4.1. The talkback.c Program
 // talkback.c -- nosy, informative program #include <stdio.h> #include <string.h> // for strlen() prototype #define DENSITY 62.4 // human density in lbs per cu ft int main() { float weight, volume; int size, letters; char name[40]; // name is an array of 40 chars printf("Hi! What's your first name?\n"); scanf("%s", name); printf("%s, what's your weight in pounds?\n", name); scanf("%f", &weight); size = sizeof name; letters = strlen(name); volume = weight / DENSITY; printf("Well, ...

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.