LESSON # 2
VARIABLE
AND DATA TYPES
Variables and
data types are building blocks of C++. A variable is a container which stores
different data types. Data types determine that what kind of data (value) can
be stored in a variable. There are several data types in C++ such as:
Numeric (5585,
953.52) can be an integer, unsigned integer, double or float.
|
|
INTEGER
|
An integer is
whole number without decimal point e.g. 5585. In C++ keyword int
is used to declare an integer. An integer can hold a number that contains 4 bytes i.e. -2000000000 to + 2000000000. For storing 8 bytes number we use unsigned int but unsigned
int only store positive numbers.
|
DOUBLE
|
Double holds a
number with decimal point e.g. 953.52. It can store a number with 8 bytes.
Keyword double is used to declare a double. We can also use float
which only takes a number of 4 bytes. In C++, to check size of a data type,
we use sizeof(data type) function. For example, cout<<sizeof(int)
returns the size of int.
|
STRING
|
String is zero
or more characters enclosed in double quotes. For example “Hello world” is
string because it has been enclosed in double quotes “ ”. Keyword string is
used to declare a string.
|
CHARACTER
|
Character is
single letter enclosed in single quotes e.g ‘d’ is a character. To declare a
character, the keyword char is
used. ( single letter e.g. ‘d’ , ‘y’)
. Remember that these values are stored as numbers which based on ASCII code.
|
BOOLEAN
|
Boolean holds
true or false and is declared with keyword bool.
|
RULES
FOR NAMING VARIABLES:
Must
begin with a letter
May
consist of letters, numbers, letter and underscore
No
space between
|
Good example:
Name, last_name; a2z
Bad example:
first
name; 4family
|
Let
illustrate these ideas with help of an example:
C++ Code
|
Explanation
|
using
namespace std;
#include<iostream>
main()
{
|
|
int
a;
|
Declaring
an integer variable with name a
|
a=5585;
|
Assigning
(storing) the data 5585 into variable a
|
double
b;
|
Declaring
a double variable with name b
|
b=953.52;
|
Assigning
(storing) the data 953.52 in into variable b
|
string
c;
|
Declaring
a string variable with name c
|
c="Hello
world";
|
Assigning
(storing) the text data hello world in into variable c
|
char
d;
|
Declaring
a char variable with name d
|
d='d';
|
Assigning
(storing) the data d in into variable d
|
cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\n";
|
Displays all variables on the screen, note we are putting all variable
together by inserting << after each variable and a tab \t character
after variable. At end \n is for next line.
|
system("pause");
}
|
|
Output
|
|
5585 953.52
Hello world d
Press any key to continue . . .
|
|
CODE
IN ACTION
Let’s do one more example:
C++ Code
|
Explanation
|
using namespace std;
#include<iostream>
main()
{
|
|
int x,y,z;
|
Declaring three int variables i.e.
x,y,z. Remember
we can declare variables in a
single line, in this case
all these three variables are of
type int.
|
x=10;
|
Assigning value
|
y=15;
|
Assigning value
|
z=x+y;
|
Assigns the values of another two
variables. Variable z holds the sum of x and y.
|
cout<<z;
|
Displays z on the screen
|
cout<<y-x;
|
Displays the result of y – z, note
that we can also do simple arithmetic operations with variables.
|
cout<<"\n";
|
Moves the cursor to next line
|
double a=5.5,b=4.5,c=a*b;
|
Declaration and initialization
(assigning values ) at the same time, it does not matter as far as we are dealing
with a single data type.
|
cout<<c;
|
Displays c on the screen
|
cout<<"\n";
|
|
cout<<sizeof(int);
|
sizeof() functions returns the size
of int
|
cout<<"\n";
|
|
system("pause");
}
|
|
Output
|
|
255
24.75
5345.654
Press any key to continue . . .
|
|
CODE
IN ACTION
No comments:
Post a Comment