- A Down-and-Dirty Chunk of Code
- The main() Function
- Kinds of Data
- Wrapping Things Up with Another Example Program
Wrapping Things Up with Another Example Program
This chapter’s goal was to familiarize you with the “look and feel” of a C program, primarily the main() function that includes executable C statements. As you saw, C is a free-form language that isn’t picky about spacing. C is, however, picky about lowercase letters. C requires lowercase spellings of all its commands and functions, such as printf().
At this point, don’t worry about the specifics of the code you see in this chapter. The rest of the book explains all the details. But it is still a great idea to type and study as many programs as possible—practice will increase your coding confidence! So here is a second program, one that uses the data types you just covered:
/* A Program that Uses the Characters, Integers, and Floating-Point Data Types */ #include <stdio.h> main() { printf("I am learning the %c programming language\n", 'C'); printf("I have just completed Chapter %d\n", 2); printf("I am %.1f percent ready to move on ", 99.9); printf("to the next chapter!\n"); return 0; }
This short program does nothing more than print three messages onscreen. Each message includes one of the three data types mentioned in this chapter: a character (C), an integer (2), and a floating-point number (99.9).
The main() function is the only function in the program written by the programmer. The left and right braces ({ and }) always enclose the main() code, as well as any other function’s code that you might add to your programs. You’ll see another function, printf(), that is a built-in C function that produces output. Here is the program’s output:
I am learning the C programming language I have just completed Chapter 2 I am 99.9 percent ready to move on to the next chapter!