Created on: 3/20/05 Last edited on: 3/21/05 LESSON 1-a: VARIABLES Section 1: VARIABLE INTRODUCTION void main() { int a; char b; double c; float d; bool e; short int a; long int b; short unsigned int a; long unsigned int b; } By now, you should know what void main() is, and that each of the lines imbetween the curly brackets {} are statements - because they end with semicolons. You shouldn't know all the rest. Each statement in this program is a declaration of a variable. These statements tell the compiler to set aside a certain amount of memory for each of these "variables". Mind you, the compiler isn't supposed to do anything else. In other languages, these variables would be set to an initializing value - usually 0. In C++, you DON'T have this guaranteed to you, though your compiler may choose to initialize them. But don't take advantage of this, as it can cause problems if you distribute your program to other people. A variable declaration follows this format: A data type, followed by a space, and then followed by a valid name, and then a semicolon. A valid name in C++ follows certain guidelines: a.) Is composed letters, numbers and underscores b.) Begins with a letter, or underscores. I STRONGLY recommend, that you ALWAYS start a variable name with a letter. However, adding an underscore is a nice way to create 2 different variables with similar names, which are meant to do 2 similar things. c.) names are CASE SENSITIVE. Everything in C++ is case sensitive. Remember this. There are 5 different basic data types in C/C++: char, int, float, double, and void. Character, Integer, Floating point, Double precision floating point, and valueless, respectively. Only the first 4 are a valid data type for variables (more on void later - when we talk about functions, and when we talk about pointers). A char, shortened from character, is the numerical value of an ASCII character. This should not be confused with a Java char, which uses the numerical value of an unicode character. When you output a variable of type char, it will be displayed on your screen as a an ASCII symbol. If I feel like it, I may have a lesson on the ASCII table, and show you what all of the ASCII symbols are, and their values. But until then, you can always search "ASCII" on google, and you should be able to find a table with relative ease. There are 4 modifiers that you may append onto some of the data types: signed unsigned long short "signed" implies that the data type can have negative values. This is redundant on ints, but still legal "unsigned" implies that the data type can only have non-negative values. This effectively doubles the maximum size a positive number can be on a variable. "long" is supposed to make a data type larger "short" is supposed to make a data type smaller Here are some definitions which will be helpful: (These are the Microsoft Visual Studios definitions, your compiler could be different) a.) The size of an int is defined by your computer. If your computer is a 32-bit machine, then an int will be 32 bits large. If your computer is a 16-bit machine, then it will be 16-bits. I do not know if 64-bit machines produce 64-bit ints, but I wouldn't be surprised either way. b.) The size of a short int is 16 bits. Period. c.) The size of a long int is 32 bits. Period. d.) A "short int" can be declared as simply a "short" e.) A "long int" can be declared as simply a "long" f.) The size of a float is 32 bits. g.) The size of a double is 64 bits. h.) The size of a "long long" is 64 bits. i.) The size of a char is 8 bits. Period A table of rangers (presumes 32-bit system): ------ -------------- --------------------------------- |type |modifiers | range | |int | | -2,147,483,648 to 2,147,483,647 | |int |long | -2,147,483,648 to 2,147,483,647 | |int |short | -32,768 to 32,767 | |int |unsigned long | 0 to 4,294,967,296 | |int |unsigned short| 0 to 65,536 | |long |long | -9,223,372,036,854,775,808 to | | | 9,223,372,036,854,775,807 | |long |unsigned long | 0 to 18,446,744,073,709,551,616 | |char | | -128 to 127 when promoted to int| |char |signed | -128 to 127 when promoted | |char |unsigned | 0 to 255 when promoted | |float | | 3.4 * 10^+/-38 (7 digits) | |double| | 1.7 * 10^+/-308 (15 digits) | |double|long | 1.7 * 10^+/-308 (15 digits) | ------ -------------- --------------------------------- It should be pointed out that all of the above examples are considered to be different types by your compiler. Though they contain the same data, your compiler may operate on these types in a different manner (may take more memory, or may take more time - but not that significant in our day and age). This is also important to keep in mind while "casting" - explained later. Examples of different variables: short int innt; // long int Innt; //different from innt double decimal; long double Decimal; //though the same values can be contained in //decimal, the compiler treats the 2 variables as //different types. char _myChar; signed char _mychar; //different from _myChar float deci; //smaller than decimal or Decimal The following program should now make total and complete sense to you: (if you actually try to compile this program, your compiler may give you a warning or an error, saying that you have unused declared variables. This depends on the compiler you use) //Start of file void main() { int a; int b; char c; double d; short e; float f; } //End of file Section 2: C++ VARIABLES In C++, a variable holds more information than just a number. It also holds an address. When your computer accesses a variable, it only has access to the address of a variable. As humans, we can easily solve this problem. Just take the address of the variable, and go there, and fetch the value located there. And that is exactly what happens. A diagram that is sometimes used to help people learn this fact is shown below: ___________ | int i | |0xcdff31f0 | --------------------------------- |___________| | | V _____________________________ | address:0xcdff31f0 | | value:5,064 | |_____________________________| The box on the left is the address of another address. This address is located very close to the addresses of all other variables, and are part of a data structure called a "stack". Why not put the values there instead of more addresses you might ask? Because the addresses of all variables are all the same size, no matter what type they are, and it would be difficult to access other information that is stored in the stack if the size of each piece of data were a different size. Therefore, it would be best if we only stored addresses on the stack, and not actual values. It is just easier, and faster, for a computer to keep track of variables this way. You don't have to understand anything about the "stack", just understand that each variable is composed of a value, and an addresss to that value.