Chapter 4. Basic Declarations and Expressions
A journey of a thousand miles must begin with a single step.
If carpenters made buildings the way programmers make programs, the first woodpecker to come along would destroy all of civilization.
Elements of a Program
If you are going to construct a building, you need two things: the bricks and a blueprint that tells you how to put them together. In computer programming, you need two things: data (variables) and instructions (code or functions). Variables are the basic building blocks of a program. Instructions tell the computer what to do with the variables.
Comments are used to describe the variables and instructions. They are notes by the author documenting the program so that the program is clear and easy to read. Comments are ignored by the computer.
In construction, before we can start, we must order our materials: “We need 500 large bricks, 80 half-size bricks, and 4 flagstones.” Similarly, in C, we must declare our variables before we can use them. We must name each one of our “bricks” and tell C what type of brick to use.
After our variables are defined, we can begin to use them. In construction, the basic structure is a room. By combining many rooms, we form a building. In C, the basic structure is a function. Functions can be combined to form a program.
An apprentice builder does not start out building the Empire State Building, but rather starts on a one-room house. In this chapter, we will concentrate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access