DATA TYPES AND VARIABLES
Before reading this tutorial, you must read First C++ Program.
CPU AND MEMORY
A computer acts on some data according to the instructions specified in a program (like our previous C++ program) and produces the output. This computation is done by Central Processing Unit(CPU) of the computer. A CPU of computer performs arithmetic operations such as addition, subtraction, multiplication and division and also performs logical operation on the data such as comparing two numbers.
Memory of a computer is the area where data and programs are stored. A computer memory is made up of registers and cells which are capable of holding information in the form of binary digits 0 and 1 (bits). This is the way information is stored in a memory just like we memorize numbers using 0 to 9. Collection of 8 bits is called a byte. Normally the CPU does not accessed memory by individual bits. Instead, it accesses data in a collection of bits, typically 8 bits, 16 bit, 32 bit or 64 bit. The amount if bits on which it can operate simultaneously is known as the word length of the computer. When we say that Pentium 4 is a 32 bit machine, it means that it simultaneously operates on 32 bit of data. Data is stored in the memory at physical memory locations. These locations are known as the memory address. It is very difficult for humans to remember the memory addresses in numbers like 46735, so instead we name a memory address by a variable.
Variables are a way of reserving memory to hold some data and assign names to them so that we don't have to remember the numbers like 46735 and instead we can use the memory location by simply referring to the variable. Every variable is mapped to a unique memory address. For example, we have 3 variable v1, v2, v3. They may be assigned the memory addresses 32000, 12456, 67893 respectively. Here is the illustration.
v1 v2 v3
32000 12456 67893
It specifies that there are three variables v1, v2, v3 (named by programmer) and have assigned the memory locations 32000, 12456 and 67893. These memory locations are not their values. The values of the variables are what you assign to them. You can assign value 86 to v1, 32.45 to v2 and 'a' to v3.
Declaring and defining variables
A variable in C++ must be declared (the type of variable) and defined (values assigned to a variable) before it can be used in a program. Following shows how to declare a variable.
Every variable in C++ can store a value. However, the type of value which the variable can store has to be specified by the programmer. C++ supports the following inbuilt data types:- int (to store integer values), float (to store decimal values) and char (to store characters), bool (to store Boolean value either 0 or 1) and void (signifies absence of information).
Integer data type
Integer (int) variables are used to store integer values like 34, 68704 etc. To declare a variable of type integer, type keyword int followed by the name of the variable. You can give any name to a variable but there are certain constraints, they are specified in Identifiers section. For example, the statement
declares that sum is a variable of type int. You can assign a value to it now or later. In order to assign values along with declaration use the assignment operator (=).
int sum = 25;
assigns value 25 to variable sum.
There are three types of integer variables in C++, short, int and long int. All of them store values of type integer but the size of values they can store increases from short to long int. This is because of the size occupied by them in memory of the computer. The size which they can take is dependent on type of computer and varies. More is the size, more the value they can hold. For example, int variable has 2 or 4 bytes reserved in memory so it can hold 2 32= 65536 values. Variables can be signed or unsigned depending they store only positive values or both positive and negative values. And short, variable has 2 bytes. Long int variable has 4 bytes.
Float data type
To store decimal values, you use float data type. Floating point data types comes in three sizes, namely float, double and long double. The difference is in the length of value and amount of precision which they can take and it increases from float to long double. For example, statement
float average = 2.34;
declares a variable average which is of type float and has the initial value 2.34
Character data type
A variable of type char stores one character. It size of a variable of type char is typically 1 byte. The statement
char name = 'c';
declares a variable of type char (can hold characters) and has the initial values as character c. Note that value has to be under single quotes.
A variable of type bool can store either value true or false and is mostly used in comparisons and logical operations. When converted to integer, true is any non zero value and false corresponds to zero.
You can find typical size of all data types here.
Keywords are the reserved words in any language which have a special pre defined meaning and cannot be used for any other purpose. You cannot use keywords for naming variables or some other purpose. We saw the use of keywords main, include, return, int in our first C++ program.
Identifiers are the name of functions, variables, classes, arrays etc. which you create while writing your programs. Identifiers are just like names in any language. There are certain rules to name identifiers in C++. They are:-
Identifier names can contain any combination of characters as opposed to the restriction of 31 letters in C.
Constants are fixed values which cannot change. For example 123, 12.23, 'a' are constants.
Now it's time to move on to our next tutorial Input values using cin operator.
Go to the previous lesson or proceed to the next lesson