Define variable? what are the two values associated with a variable
Answers
Answered by
1
What Is a Variable?
From a programmer's point of view, a variable is a location in your computer's memory in which you can store a value and from which you can later retrieve that value.
To understand this, you must first understand a bit about how computer memory works. Your computer's memory can be thought of as a series of cubby holes, all lined up in a long row. Each cubby hole—or memory location—is numbered sequentially. These numbers are known as memory addresses.
Variables not only have addresses, they have names. For example, you might create a variable named myAge. Your variable is a label on one of these cubby holes so that you can find it easily, without knowing its actual memory address.
Figure 3.1 is a visual representation of this idea. As you can see from the figure, we've declared a variable named myVariable. myVariable starts at memory address 103.
Figure 3.1 A visual representation of memory.
New Term
RAM is Random Access Memory. It is the electronic memory your computer uses while executing programs. Any information in RAM is lost when your computer is turned off. When you run your program, it is loaded into RAM from the disk file (hard drive storage). All variables are created in RAM as well. When programmers talk of memory, it is usually RAM to which they are referring.
Setting Aside Memory
When you define a variable in C++, you must tell the compiler not only what its name is, but also what kind of information it will hold: integer, character, and so forth. This is the variable's type. The type of the variable tells the compiler how much room to set aside in memory to hold the variable's value.
Each cubby is one byte large. If the type of variable you create is two bytes in size, it needs two bytes of memory, or two cubbies. The type of the variable (for example, int) tells the compiler how much memory (how many cubby holes) to set aside for the variable. Because computers use bits and bytes to represent values, and because memory is measured in bytes, it is important that you understand and are comfortable with these concepts.
Size of Integers
A char variable (used to hold characters) is most often one byte long. A short int is two bytes on most computers; a long int is usually four bytes, and an int (without the keyword short or long) can be two or four bytes. If you are running Windows 95, Windows 98, or Windows NT, you can count on your int being four bytes as long as you use a modern compiler.
Listing 3.1 will help you determine the exact size of these types on your computer using your particular compiler.
Listing 3.1 Determines the Size of Variable Types on Your Computer.
0: #include <iostream>
1:
2: int main()
3: {
4: std::cout << "The size of an int is:\t\t";
5: std::cout << sizeof(int) << " bytes.\n";
6: std::cout << "The size of a short int is:\t";
7: std::cout << sizeof(short) << " bytes.\n";
8: std::cout << "The size of a long int is:\t";
9: std::cout << sizeof(long) << " bytes.\n";
10: std::cout << "The size of a char is:\t\t";
11: std::cout << sizeof(char) << " bytes.\n";
12: std::cout << "The size of a float is:\t\t";
13: std::cout << sizeof(float) << " bytes.\n";
14: std::cout << "The size of a double is:\t";
15: std::cout << sizeof(double) << " bytes.\n";
16:
17: return 0;
18: }
OUTPUT
The size of an int is 2 bytes.
The size of a short int is 2 bytes.
The size of a long int is 4 bytes.
The size of a char is 1 bytes.
The size of a bool is 1 bytes.
The size of a float is 4 bytes.
The size of a double is 8 bytes.
NOTE
On your computer, the number of bytes presented might be different!
Analysis - Most of Listing 3.1 will be familiar. The one new feature is the use of the sizeof() function in lines 5 through 15. sizeof() is provided by your compiler, and it tells you the size of the object you pass in as a parameter. For example, on line 5 the keyword int is passed into sizeof(). Using sizeof(), I was able to determine that on my computer, an int is equal to a short int, which is two bytes.
signed and unsigned
In addition, all these types come in two varieties: signed and unsigned. Sometimes you need negative numbers, and sometimes you don't. Integers (short and long) without the word "unsigned" are assumed to be signed. signed integers are either negative or positive. unsigned integers are always positive.
Because you have the same number of bytes for both signed and unsigned integers, the largest number you can store in an unsigned integer is twice as big as the largest positive number you can store in a signed integer. An unsigned short integer can handle numbers from 0 to 65,535. Half the numbers represented by a signed short are negative, thus a signed short can only represent numbers from –32,768 to 32,767.
Fundamental Variable Types
Several other variable types are built into C++. They can be conveniently divided into integer variables (the type discussed so far), floating-point variables, and character
From a programmer's point of view, a variable is a location in your computer's memory in which you can store a value and from which you can later retrieve that value.
To understand this, you must first understand a bit about how computer memory works. Your computer's memory can be thought of as a series of cubby holes, all lined up in a long row. Each cubby hole—or memory location—is numbered sequentially. These numbers are known as memory addresses.
Variables not only have addresses, they have names. For example, you might create a variable named myAge. Your variable is a label on one of these cubby holes so that you can find it easily, without knowing its actual memory address.
Figure 3.1 is a visual representation of this idea. As you can see from the figure, we've declared a variable named myVariable. myVariable starts at memory address 103.
Figure 3.1 A visual representation of memory.
New Term
RAM is Random Access Memory. It is the electronic memory your computer uses while executing programs. Any information in RAM is lost when your computer is turned off. When you run your program, it is loaded into RAM from the disk file (hard drive storage). All variables are created in RAM as well. When programmers talk of memory, it is usually RAM to which they are referring.
Setting Aside Memory
When you define a variable in C++, you must tell the compiler not only what its name is, but also what kind of information it will hold: integer, character, and so forth. This is the variable's type. The type of the variable tells the compiler how much room to set aside in memory to hold the variable's value.
Each cubby is one byte large. If the type of variable you create is two bytes in size, it needs two bytes of memory, or two cubbies. The type of the variable (for example, int) tells the compiler how much memory (how many cubby holes) to set aside for the variable. Because computers use bits and bytes to represent values, and because memory is measured in bytes, it is important that you understand and are comfortable with these concepts.
Size of Integers
A char variable (used to hold characters) is most often one byte long. A short int is two bytes on most computers; a long int is usually four bytes, and an int (without the keyword short or long) can be two or four bytes. If you are running Windows 95, Windows 98, or Windows NT, you can count on your int being four bytes as long as you use a modern compiler.
Listing 3.1 will help you determine the exact size of these types on your computer using your particular compiler.
Listing 3.1 Determines the Size of Variable Types on Your Computer.
0: #include <iostream>
1:
2: int main()
3: {
4: std::cout << "The size of an int is:\t\t";
5: std::cout << sizeof(int) << " bytes.\n";
6: std::cout << "The size of a short int is:\t";
7: std::cout << sizeof(short) << " bytes.\n";
8: std::cout << "The size of a long int is:\t";
9: std::cout << sizeof(long) << " bytes.\n";
10: std::cout << "The size of a char is:\t\t";
11: std::cout << sizeof(char) << " bytes.\n";
12: std::cout << "The size of a float is:\t\t";
13: std::cout << sizeof(float) << " bytes.\n";
14: std::cout << "The size of a double is:\t";
15: std::cout << sizeof(double) << " bytes.\n";
16:
17: return 0;
18: }
OUTPUT
The size of an int is 2 bytes.
The size of a short int is 2 bytes.
The size of a long int is 4 bytes.
The size of a char is 1 bytes.
The size of a bool is 1 bytes.
The size of a float is 4 bytes.
The size of a double is 8 bytes.
NOTE
On your computer, the number of bytes presented might be different!
Analysis - Most of Listing 3.1 will be familiar. The one new feature is the use of the sizeof() function in lines 5 through 15. sizeof() is provided by your compiler, and it tells you the size of the object you pass in as a parameter. For example, on line 5 the keyword int is passed into sizeof(). Using sizeof(), I was able to determine that on my computer, an int is equal to a short int, which is two bytes.
signed and unsigned
In addition, all these types come in two varieties: signed and unsigned. Sometimes you need negative numbers, and sometimes you don't. Integers (short and long) without the word "unsigned" are assumed to be signed. signed integers are either negative or positive. unsigned integers are always positive.
Because you have the same number of bytes for both signed and unsigned integers, the largest number you can store in an unsigned integer is twice as big as the largest positive number you can store in a signed integer. An unsigned short integer can handle numbers from 0 to 65,535. Half the numbers represented by a signed short are negative, thus a signed short can only represent numbers from –32,768 to 32,767.
Fundamental Variable Types
Several other variable types are built into C++. They can be conveniently divided into integer variables (the type discussed so far), floating-point variables, and character
Similar questions
Physics,
7 months ago
French,
7 months ago
English,
7 months ago
Physics,
1 year ago
Political Science,
1 year ago