Home > Articles > Programming

Storing Information: Variables and Constants in C

  • Print
  • + Share This
Begin to understand how your computer stores information and then find out how to best use those memory allocations when choosing your variables and constants.
This chapter is from the book

Computer programs usually work with different types of data and need a way to store the values being used. These values can be numbers or characters. C has two ways of storing number values—variables and constants—with many options for each. A variable is a data storage location that has a value that can change during program execution. In contrast, a constant has a fixed value that can't change. Today you will learn:

  • How to store information using variables in C

  • Ways to efficiently store different types of numeric values

  • The differences and similarities between character and numeric values

  • How to declare and initialize variables

  • C's two types of numeric constants

Before you get to variables, however, you need to know a little about the operation of your computer's memory.

Understanding Your Computer's Memory

If you already know how a computer's memory operates, you can skip this section. If you're not sure, read on. Understanding your computer's memory and how it works will help you better understand certain aspects of C programming.

A computer uses random-access memory (RAM) to store information while it is operating. RAM is generally located inside your computer. RAM is volatile, which means that it is erased and replaced with new information as often as needed. Being volatile also means that RAM "remembers" only while the computer is turned on and loses its information when you turn the computer off.

Each computer has a certain amount of RAM installed. The amount of RAM in a system is usually specified in megabytes (MB), such as 2MB, 4MB, 8MB, 32MB or more. One megabyte of memory is 1,024 kilobytes. One kilobyte of memory consists of 1,024 bytes. Thus, a system with 4MB of memory actually has 4x1,024 kilobytes, or 4,096 kilobytes of RAM. This would be 4,096KBx1,024 bytes for a total of 4,194,304 bytes of RAM.

A byte is the fundamental unit of computer data storage. Day 20, "Working with Memory," has more information about bytes. For now, Table 3.1 provides you with an idea of how many bytes it takes to store certain kinds of data.

Table 3.1 Memory space required to store data

Data

Bytes Required

The letter x

1

The number 500

2

The number 241.105

4

The phrase Sams Teach Yourself C

22

One typewritten page

Approximately 3,000


The RAM in your computer is organized sequentially, one byte following another. Each byte of memory has a unique address that can be used to identify it. This address can be used to distinguishes the byte of memory from all other bytes. Addresses are assigned to memory locations in order, starting at zero and increasing to the system limit. For now, you don't need to worry about addresses; it's all handled automatically by the C compiler.

What is your computer's RAM used for? It has several uses, but only data storage need concern you as a programmer. Data is the information with which your C program works. Whether your program is maintaining an address list, monitoring the stock market, keeping a household budget, or tracking the price of hog bellies, the information (names, stock prices, expense amounts, or hog futures) is kept in your computer's RAM while the program is running.

Now that you understand a little about the nuts and bolts of memory storage, you can get back to C programming and how C uses memory to store information.

  • + Share This
  • 🔖 Save To Your Account