First Steps in (C) Programming, introduction
(Page 1 of 8 )
If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the first of three parts, is excerpted from chapter two of the book
Beginning C, written 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.
/mini-image_1.JPG)
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.
Next: What Is a Variable? >>
More C++ Articles
More By Apress Publishing
|
This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.
|
|