Storing Information: Variables and Constants in C (一)
2004-12-23 15:28
267 查看
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.
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.
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.
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 |
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.
相关文章推荐
- Storing Information: Variables and Constants in C (二)
- Storing Information: Variables and Constants in C (三)
- Templates and Static variables in C++
- OOP in JS Public/Private Variables and Methods
- [CSSinJS] Convert Sass (SCSS) Styled Button to CSSinJS with JavaScript Templates and Variables
- More on Variables and Data Types(Chapter 10 of Programming in Objective-C 2.0)
- MZ, BSJB, and the Joys of "Magic" Constants in Managed Assemblies.
- [Python] How to pack and unpack variables in Python?
- Java Tip 27: Typesafe constants in C++ and Java
- Creating Protected and Private Variables in Objective-C Class
- constants and fields in c#
- Swift from Scratch: Variables and Constants
- Checking for Undefined, Null, and Empty Variables in JavaScript
- Understanding transient variables in Java and how they are practically used in HashMap---reference
- Declare Variables, Constants and Types in Oracle PL/SQL
- what is the difference between static and normal variables in c++
- [译]5.11. Functions and Variables Featured in This Chapter 本章的函数,变量和特性
- Chapter 2: Variables and Constants
- Esper系列(十二)Variables and Constants
- OOP in JS, Part 1 : Public/Private Variables and Methods