First Steps in Programming
(Page 1 of 13 )
This article will show you how to write programs that can access and and manipulate data, and work with different data values each time you use them. It is excerpted from the book
Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530).
BY NOW YOU’RE PROBABLY EAGER to create programs that allow your computer to really interact with the outside world. You don’t just want programs that work as glorified typewriters, displaying fixed information that you included in the program code, and indeed there’s a whole world of programming that goes beyond that.
Ideally, you want to be able to enter data from the keyboard and have the program squirrel it away somewhere. This would make the program much more versatile. Your program would be able to access and manipulate this data, and it would be able to work with different data values each time you executed it. This idea of entering different information each time you run a program is key to the whole enterprise of programming. A place to store an item of data that varies in a program is, not altogether surprisingly, called a variable, and that is what this chapter covers.
This is quite a long chapter, and it covers a lot of ground. By the time you reach the end of it, you’ll be able to write some really useful programs.
In this chapter you’ll learn
- How memory is used and what variables are
- How you can calculate in C
- What different types of variables there are and what you use them for
- What casting is and when you need to use it
- How to write a program that calculates the height of a tree—any tree
Memory in Your Computer First let’s look at how the computer stores the data that’s processed in your program. To understand this, you need to know a little bit about memory in your computer, so before you go into your first program, let’s take a quick tour of your computer’s memory.
The instructions that make up your program, and the data that it acts upon, have to be stored somewhere while your computer is executing that program. When your program is running, this storage place is the machine’s memory. It’s also referred to as main memory, or the random access memory (RAM) of the machine.
Your computer also contains another kind of memory called read-only memory (ROM). As its name suggests, you can’t change ROM; you can only read its contents or have your machine execute instructions contained within it. The information contained in ROM was put there when the machine was manufactured. This information is mainly programs that control the operation of the various devices attached to your computer, such as the display, the hard disk drive, the keyboard, and the floppy disk drive. On a PC, these programs are called the basic input/output system (BIOS) of your computer. I don’t need to refer to the BIOS in detail in this book. The interesting memory for your purposes is RAM: this is where your programs and data are stored when they execute. So let’s understand a bit more about it.
You can think of your computer’s RAM as an ordered sequence of boxes. Each of these boxes is in one of two states: either the box is full when it represents 1 or the box is empty when it represents 0. Therefore, each box represents one binary digit, either 0 or 1. The computer sometimes thinks of these in terms of true and false: 1 is true and 0 is false. Each of these boxes is called a bit, which is a contraction of “binary digit.”
NOTE If you can’t remember or have never learned about binary numbers, and you want to find out a little bit more, you’ll find more detail in Appendix A. However, you needn’t worry about these details if they don’t appeal to you. The important point here is that the computer can only deal with 1s and 0s—it can’t deal with decimal numbers directly. All the data that your program works with, including the program instructions themselves, will consist of binary numbers internally.
For convenience, the boxes or bits in your computer are grouped into sets of eight, and each set of eight bits is called a byte. To allow you to refer to the contents of a particular byte, each byte has been labeled with a number, starting from 0 for the first byte, 1 for the second byte, and going up to whatever number of bytes you have in your computer’s memory. This label for a byte is called its address. Thus, each byte will have an address that’s different from that of all the other bytes in memory. Just as a street address identifies a particular house uniquely by its house number, the address of a byte uniquely references that byte in your computer’s memory.
To summarize, you have your memory building blocks (called bits) that are in groups of eight (called bytes). A bit can only be either 1 or 0. This is illustrated in Figure 2-1.

Figure 2-1. Bytes in memory
Memory is often expressed in terms of so many kilobytes, megabytes, or even gigabytes. Here’s what those words mean:
- 1 kilobyte (or 1KB) is 1,024 bytes.
- 1 megabyte (or 1MB) is 1,024 kilobytes, which is 1,048,576 bytes.
- 1 gigabyte (or 1GB) is 1,024 megabytes, which is 1,073,741,841 bytes.
You might be wondering why you don’t work with simpler, more rounded numbers, such as a thousand, or a million, or a billion. The reason is that there are 1,024 numbers from 0 to 1,023, and 1,023 happens to be 10 bits that are all 1 in binary: 11 1111 1111, which is a very convenient binary value. So while 1,000 is a very convenient decimal value, it’s actually rather inconvenient in a binary machine—it’s 11 1110 1000, which is not exactly neat and tidy. The kilobyte (1,024 bytes) is therefore defined in a manner that’s convenient for your computer, rather than for you. Similarly, for a megabyte, you need 20 bits, and for a gigabyte, you need 30 bits. One point of confusion can arise here, particularly with disk drive capacities. Disk drive manufacturers often refer to a disk as having a capacity of 537 megabytes or 18.3 gigabytes, when they really mean 537 million bytes and 18.3 billion bytes. Of course, 537 million bytes is only 512 megabytes and 18.3 billion bytes is only 17 gigabytes.
Now that you know a bit about bytes, let’s see how you can use this memory in your programs.
What Is a Variable? A variable is a specific piece of memory in your computer that consists of one or more contiguous bytes. Every variable has a name, and you can use that name to refer to that place in memory to retrieve what it contains or store a new data value there.
Let’s start with a program that displays your salary using the printf()function that you saw in Chapter 1. Assuming your salary is $10,000, you can already write that program very easily:
/* Program 2.1 What is a Variable? */
#include <stdio.h>
void main()
{
printf("My salary is $10000");
}
I’m sure you don’t need any more explanation about how this works; it’s almost identical to the programs you developed in Chapter 1. So how can you modify this program to allow you to customize the message depending on a value stored in memory? There are, as ever, several ways of doing this. What they all have in common, though, is that they use a variable.
In this case, you could allocate a piece of memory that you could call, say, salary, and store the value 10000 in it. When you want to display your salary, you could use the name you’ve given to the variable, which is salary, and the value that’s stored in it (10000) would be displayed. Wherever you use a variable name in a program, the computer accesses the value that’s stored there. You can access a variable however many times you need to in your program. And when your salary changes, you can simply change the value stored in the variable salary and the whole program will carry on working with the new value. Of course, all these values will be stored as binary numbers inside the computer.
You can have as many variables as you like in a program. The value that each variable contains, at any point during the execution of that program, is determined by the instructions contained in your program. The value of a variable isn’t fixed, and it can change as many times as you need it to throughout a program.
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Variables That Store Numbers >>
More Code Examples Articles
More By Apress Publishing