Data Types and Variables
Data Types and Variables
Before reading this tutorial, you must read First C++ Program.
VARIABLES
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.
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.
DATA TYPES
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 (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
int sum;
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 (=).
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.
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
declares a variable average which is of type float and has the initial value
2.34
A variable of type char stores one character. It size of a variable of type char
is typically 1 byte. The statement
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.
Boolean
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.
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
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:-
CONSTANTS
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.