CH 2 - Concept Of Data Types
Variables
• Variable is a name given to memory location where data is stored or
needs to be stored.
• All the variables must be declared before they can used.
• Syntax:
<data type> <Variable Name>=<value >;
• Example:
int a=5;
int a=5, b=6,c=7;
float e=2 . 3f;
How to Initialize Variables in C++?
• It can be perceived with the help of 3 components that are as
follows:
• datatype: Type of data that can be stored in this variable.
• variable_name: Name given to the variable.
• value: It is the initial value stored in the variable.
Rule of Naming a Variable
• It can have any letter of alphabet[capital or small], digits ,
underscore.
For examples a,b,Cat,Mat123,Cost_prise,Student$
• Variable name should be meaningful, so that it is easily tell the
reader what the variable is used for!
• It can be lengthy.
• It should not begin with digits or should not contains
any special characters.
For examples
2ab,ab#c,top@
• IT can not have space between it.
For examples
Simple Interest
• It must not be a keyword/reserved word.
For example
for, while, do
• Case sensitive
For example, demo is not the same name as Demo
Literals(Constants)
• Literals are the constants used in a C++ program.
• When you write a program in C++, you may come across some
values, which remain fixed(i.e. Do not change) throughout the
execution of the program. Such values are called literals or
constants.
variable
Data Types
• There are several data types available in c++
• The Basic built in data types are
1. char
2. int
3. float
4. double
5. bool
Built in data types
• char: For characters. Size 1 byte.
char ch = 'A';
• int: For integers. Size 2 bytes.
int num = 100;
• float: Used for storing numbers that have fractional part. Size 4 bytes.
float num = 123.78987;
• double: Used for storing numbers that have fractional part. Size 8 bytes.
double num = 10098.98899;
• bool: For booleans, true or false.
bool b = true;
bool The bool type takes one byte and stores a value of true (1) or false(0).
int
• The keyword int used for storing integer value.
// C++ Program demonstrate Use of int
#include <iostream>
using namespace std;
int main ()
{
int num = 10;
cout << num;
return 0;
}
Small integer
Size=2 bytes
Range = -32768 to 32767
Long Integer
Size=4 bytes
Range = -2147483648 to 2147483648
For example,
int salary = 85000;
Int num=10;
char
• The char data type is used to store a single character. The character must be surrounded by single
quotes, like 'A' or 'c':
• The keyword used for the character data type is char.
• Characters typically require 1 byte of memory.
// C++ Program demonstrate Use of char
#include <iostream>
using namespace std;
int main ()
{
char myGrade = 'B';
cout << myGrade;
return 0;
}
float
• The keyword float is used for storing numbers that have fractional part. For Example 12.55
• Float variables typically require 4 bytes of memory.
• Sufficient for storing 6-7 decimal digits
#include <iostream>
using namespace std;
int main ()
{
float num = 10.2;
cout << num;
return 0;
}
double
• double keyword is used for storing numbers that have fractional part. For Example 12.55
• Double variables typically require 8 bytes of memory space.
• Sufficient for storing 15 decimal digits
#include <iostream>
using namespace std;
int main ()
{
double num = 10.25666735;
cout << num;
return 0;
}
bool
• The keyword bool is used for storing Boolean values.
• A Boolean values can either true or false.
#include <iostream>
using namespace std;
int main ()
{
// Creating variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
// Print variable values
cout << myNum ;
cout << myFloatNum ;
cout << myDoubleNum ;
cout << myLetter;
cout << myBoolean ;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
// Creating variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
// Print variable values
cout << "int: " << myNum << "\n";
cout << "float: " << myFloatNum << "\n";
cout << "double: " << myDoubleNum << "\n";
cout << "char: " << myLetter << "\n";
cout << "bool: " << myBoolean << "\n";
return 0;
}
Keywords
• Keywords in C++ are reserved words that have a strict meaning
and may not be used in any other way.
• For example,
int , char , double , float