Chapter Three 2
Chapter Three 2
All variables use data-type during declaration to restrict the type of data to
be stored.
Therefore, we can say that data types are used to tell the variables the type
of data it can store.
Every data type requires a different amount of memory.
Based on the data type of a variable, the operating system allocates memory
and decides what can be stored in the reserved memory.
Data types
Primitive Data Types: These data types are built-in or predefined data
types and can be used directly by the user to declare variables.
Primitive data types available in C++ are:
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Data types
Integer (int):
Stores (positive, negative, or zero), or stores number without decimal point.
Examples: int age = 25, int score = -78, int itemCount = 0
Floating-Point Number (float, double):
Stores numbers with decimal points.
Examples: float pi = 3.14159 , float f=-78.45
using double for better accuracy, doble d=10.14454848
Character (char):
Stores a single character, typically represented by a letter, number, symbol, or
punctuation mark.
Uses single quotes (') to enclose the character.
Examples: char initial = 'A’, char symbol = '&’, char digit = '7’
Boolean (bool):
stores logical values: true or false.
Examples: bool isRegistered = true
signed and unsigned Modifiers
Signed variables can hold both positive and negative integers including zero. For
example,
Here,
• x holds a positive-valued integer
• y holds a negative-valued integer
• z holds a zero-valued integer
signed and unsigned Modifiers
The unsigned variables can hold only non-negative integer values. For
example,
Here,
• x holds a positive-valued integer
• y holds zero
5
Data types
Next you have a summary of the basic fundamental data types in C++, as well as the
range of values that can be represented with each one:
• The sizes of variables might be different from those shown in the above
table, depending on the compiler and the computer you are using.
• Following is the example, which will produce correct various data type 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;
}
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
In C++ there the basic operators are the following:
Arithmetic Operators
Increase and Decrease Operators
Assignment Operators
Logical Operators
Relational Operators
Conditional Operator
Bitwise Operators
8
Arithmetic operators
• The five arithmetical operations supported by the C++ language are:
Symbols Descriptions
+ addition
- subtraction
* multiplication
/ division
% modulo
return 0;
}
Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values C = A + B will assign
from right side operands to left side operand value of A + B into C
c+=1;
c=c+1
are all equivalent in its functionality: the three of them increase by one
the value of c.
Example
• Increment operator(++) and decreament(--)
• Can be placed before the variable(prefix form)
• ++i; int i=21; int i=21;
• --i; ++i; --i;
cout<<i; //22 cout<<i; //20
• Can be placed after the variable (postfix form)
• i++;
int i=21; int i=21;
• i-- i++; i--;
cout<<i; // 22 cout<<i; //20
Example
• ++ x and x++ difference
int a=21;
int b=++a;
cout<<"a is :"<<a<<" b is: "<<b; // a is 22 b is 22
• ++X increments the value of x by 1 and uses the new value in the
statement
int a=21;
int b=a++;
cout<<"a is :"<<a<<" b is: "<<b; // a is 22 b is 21
• X++ increments the value of x by 1 and use es the original value in the
statement
int a=21; // a is 22 b is 25
int b=a++ + 4;
cout<<"a is :"<<a<<" b is: "<<b;
int a=21;
int b=++a + 4; // a is 22 b is 26
cout<<"a is :"<<a<<" b is: "<<b;
Example
• --X and X-- difference
int a=21;
int b=--a;
cout<<"a is :"<<a<<" b is: "<<b; // a is 20 b is 20
• ++X increments the value of x by 1 and uses the new value in the
statement
int a=21;
int b=a--;
cout<<"a is :"<<a<<" b is: "<<b; // a is 20 b is 21
• X++ increments the value of x by 1 and use es the original value in the
statement
int a=21; // a is 20 b is 25
int b=a-- + 4;
cout<<"a is :"<<a<<" b is: "<<b;
int a=21;
int b=++a + 4; // a is 20 b is 24
cout<<"a is :"<<a<<" b is: "<<b;
Logical Operators
Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.
Logical operators in C++ are: &&(and operator), ||(or operator), and !(not
operator)
Let’s say we have two Boolean variables b1 and b2.
b1&&b2 will return true if both b1 and b2 are true else it would return
false.
b1||b2 will return false if both b1 and b2 are false else it would return true.
!b1 would return the opposite of b1, that means it would be true if b1 is
false and it would return false if b1 is true.
Logical Operators Example
Logical Operators Example
int a=5;
if(a==5 && a!=6 && a<=10){
cout<<“Hello World"; //Hello World
}
int a=5;
if(a!=5||a==6 || a<=10){ //Hello World
cout<<“Hello World";
}
int a=5;
if(!(a==6)){ //Hello World
cout<<“Hello World";
}
Relational operators
In C++, Relational Operators are used for comparing two or more numerical
values.
All relational operators returns either True or False
We have six relational operators in C++: ==, !=, >, <, >=, and <=
== returns true if both the left side and right side operands are equal
!= returns true if left side operand is not equal to the right side operand
> returns true if left side operand is greater than right side operand.
< returns true if left side operand is less than right side operand.
>= returns true if left side operand is greater than or equal to right side operand.
<= returns true if left side operand is less than or equal to right side operand.
Relational operators
Assume variable A holds 10 and variable B holds 20, then
A==B is False
A!=B is True
A>B is False
A<B is True
A>=B is False
A<=B is true
1
0
Conditional (Ternary) Operator ( ? )
• The conditional operator evaluates an expression returning a value if
that expression is true and a different one if the expression is evaluated
as false.
• Its format is: condition ? result1 : result2
If condition is true the expression will return result1, if it is not it
will return result2.
Conditional (Ternary) Operator ( ? )
int n=180;
if(n%2==0){
cout<<"the number is even";
}
else
{
cout<<"the number is odd";
}
Th
e
s am
e
int n=180;
(n%2==0)? (cout<<"the number is even"): (cout<<"the number is odd");
Basic Input / Output in C++
In C++ input and output are performed in the form of a sequence of bytes or more
commonly known as streams.
• Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.
• Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory
to device( display screen ) then this process is called output.
The two instances cout in C++ and cin in C++ of iostream class are used very often
for printing outputs and taking inputs respectively. These two are the most basic
methods of taking input and printing output in C++. To use cin and cout in C++ one
must include the header file iostream in the program.
The cout is used in conjunction with the stream insertion operator, which is written as
<< which are two less than signs
The cin is used in conjunction with the stream extraction operator, which is written as
24
>> which are two greater than signs
Basic Input / Output in C++
#include <iostream>
The basic form for writing output to the screen is
cout << ...something... ;
For variables, simply put the variable name.
cout << MyVariableName;
For strings, use the double quotes
cout << "This will appear on the monitor";
For newlines, use the endl keyword
cout << endl;
Multiple items can be run together, e.g.
int X = 17; int Y = 23;
cout << "The value of variable X is " << X << endl;
cout << "... and Y is" << Y << endl;
This would produce output
The value of variable X is 17 25
... and Y is23