Home arrow C++ arrow First Steps in (C) Programming, introduction

First Steps in (C) Programming, introduction

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).

Author Info:
By: Apress Publishing
Rating: 3 stars3 stars3 stars3 stars3 stars / 10
November 17, 2005
  1. · First Steps in (C) Programming, introduction
  2. · What Is a Variable?
  3. · Variables That Store Numbers
  4. · Try It Out: Using More Variables
  5. · Naming Variables
  6. · Initializing Variables
  7. · Basic Arithmetic Operations
  8. · Try It Out: Division and the Modulus Operator

print this article

First Steps in (C) Programming, introduction
(Page 1 of 8 )

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.Ē

  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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials