Data Types and Variables - Copy
Data Types and Variables - Copy
(PRP-211)
The size of variables might be different from those shown in the above
table, depending on the compiler and the computer you are using.
Data Types (Cont’d)
This code will produce correct size of various data types on your computer.
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
Data Types (Cont’d)
This example uses endl, which inserts a new-line character after every
line.
<< operator is being used to pass multiple values out to the screen.
Using sizeof() function to get size of various data types.
Note:
When the code is compiled and executed, it produces the result which
can vary from machine to machine.
Variables
Variable is a location of a memory identified by a name whose content
can change.
A variable is a container which hold values in programming.
Each variable (and its content) is accessed by a name.
The name of the variable is also called identifier
A variable has
name
value and
address.
Variables (Cont’d)
Variable Definition
Is the place where the variable is created
Is allocated storage.
Variable Declaration
Is the place where nature (type) of variable is stated, but no space is
allocated.
Variable Initialization
Means assigning a value to the variable.
Variables (Cont’d)
Variables can be created many times, but defined only once.
Memory space is not allocated for a variable while declaration, it
happens only on variable definition.
Variable Declaration
To put a variable into existence is called variable declaration
Variable declaration is requesting OS to prepare a part of memory to
be used for storing program data.
A variable is declared as follows:
data_type variable_name;
For example;
int age;
This declares a variable of data type integer whose identifier/name is
age
Variable Declaration (Cont’d)
You can declare variables of the same data types in one statement as
shown below
In general:
data_type variable_name1, variable_name2;
For example;
int height, mass;
This declares variables of data type integer whose identifiers/names
are height and mass
Initializing Variables
When variable is declared (created), it can now be assigned a value.
A variable can be assigned a value as:
variable_name = value; // assuming variable_name is
declared
For example,
age = 20;
You may use a single statement to declare the variable and assign a value to
it:
data_type variable_name = value;
This is called variable initialization
Initializing Variables (Cont’d)
For example;
float height = 1.62;
This declares variable of data type float whose identifier/name is
height and initialized to 1.62.
Initializing Variables (Cont’d)
Initializing a variable also called initiating a variable is giving the
variable an initial (a starting value).
This is usually done at declaration.
They are three ways to initialize variables at declaration in C++:
i. Using =
For example int age = 20;
ii. Using ()
For example int (20);
iii. Using {}
For example int {20};
Initializing Variables (Cont’d)
If you don’t explicitly initialize numeric data types such as int, float,
double, variable is implicitly initialized to zero (0)
For example:
int age; //same as int age = 0;
cout<<age;
Displays 0.
cin and variables
Mostly data assigned to variables is entered by users of the program
(at a time program is running and not during coding).
Remember, cout object, is for displaying content.
Syntax is:
cin>>variable_name;
For example:
cin>>age;
This statement will return a cursor for a user to enter a value. When
the user types the value and presses ENTER, the value entered is
stored in variable called age.
cin and variables
Example
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<“enter your age => ”;
cin>>age; // this line is getting age from a user
cout<<“Your age is ”<<age; //this line displays age entered
return 0;
}
Constants
Constant is a location of a memory identified by a name whose
content CANNOT change.
A constant is a variable with unchangeable content.
Used to keep content that should not change at any point in a program.
Others call it a constant variable
Constant Declaration
A constant can be declared in two ways in C++:
i. Using #define
• Placed soon after #include… directives.
• Syntax:
#define CONSTANT_NAME VALUE
e.g. #define HEIGHT 1.4
Constant Declaration (Cont’d)
ii. Using keyword const
• Placed at same location as variables i.e. after int main() {
• Declaration similar to that of variables. But begins with const
keyword
• Syntax:
const data_type constant_name = value;
e.g. const float HEIGHT = 1.4;
Note:
Constant names should always be in block letters to distinguish
them from variable names.
Constant (Cont’d)
#include<iostream>
#define PI 3.14 //First Constant using #define
using namespace std;
int main()
{
const float HEIGHT = 1.4; //Second Constant using const keyword
cout<<PI<<endl;
cout<<HEIGHT;
return 0;
}
Note: You cannot assign a value to a constant after declaration.
Rules for Naming Variables and Constants
Names of variables or constants should follow the rules below:
1. Name does not contain a space.
For example: Int my age; my age is invalid identifier.
Use underscore instead e.g. int my_age;