Data Types And Operators in C++ Programming
Data Types in C++
Data type
means which type of data will be stored in a variable. There are three
primitive or basic data types in C++ programming
·
char
(stores alphabets, numbers and special characters as String)
·
int
(stores whole numbers)
·
float(stores
decimal numbers)
See the following table for more
details
Type
|
Storage size
|
Value range
|
Precision
|
char
|
1 byte
|
-128 to 127 or 0 to
255
|
NA
|
unsigned char
|
1 byte
|
0 to 255
|
NA
|
signed char
|
1 byte
|
-128 to 127
|
NA
|
int
|
2 or 4 bytes
|
-32,768 to 32,767
or -2,147,483,648 to 2,147,483,647
|
NA
|
unsigned int
|
2 or 4 bytes
|
0 to 65,535 or 0 to
4,294,967,295
|
NA
|
short
|
2 bytes
|
-32,768 to 32,767
|
NA
|
unsigned short
|
2 bytes
|
0 to 65,535
|
NA
|
long int
|
4 bytes
|
-2,147,483,648 to
2,147,483,647
|
NA
|
unsigned long
|
4 bytes
|
0 to 4,294,967,295
|
NA
|
float
|
4 byte
|
1.2E-38 to 3.4E+38
|
6 decimal places
|
double
|
8 byte
|
2.3E-308 to
1.7E+308
|
15 decimal places
|
long double
|
10 byte
|
3.4E-4932 to
1.1E+4932
|
19 decimal places
|
·
Unsigned
Means only +(positive)
·
Signed
Means both + (positive) and – (negative)
·
Long
means higher storage size
·
As
int stores the numbers between -32,768 to 32,767 but long int stores between -2,147,483,648 to 2,147,483,647
Operators in C++
Operators
are those things which operate on operands. For Example
int
a=5,b=15,c;
c=a+b;
here a,b and
c are variables or operands whereas + and = are operators.
Let us see
more explanation:
There are 3 categories of operators
in C++ programming:
·
Arithmetic
Operators
o
+
(Arithmetic Sum)
o
–
(Arithmetic Subtraction)
o
*
(Arithmetic Multiplication)
o
/
(Arithmetic Division)
o
%
(Arithmetic Modulus or Remainder)
o
=
(Arithmetic Assignment)
·
Relational
Operators
o
< (Less Than)
o
<=
(Less Than or Equal To)
o
> (Greater Than)
o
>=
(Greater Than or Equal To)
o
==
(Equal To)
o
!= (Not Equal To)
·
Logical
Operators
o
&&
(AND) (When all the joined conditions are true)
o
|| (OR) (When any of the joined conditions are
true)
o
! (NOT) (When condition is not true)
Coding Examples of Operators:
·
Arithmetic
Operators
Arithmetic operators like +, - , *(Multiplication)
and = (Assignment) are easy to understand. Here I would like to show the working of two Arithmetic
operators / and %. See the following code :
int a=15,b=6,c=0;
c=a/b; [ c will hold 2 as c is an integer variable
and does not hold decimal ]
c=a%b; [c will hold 3 as % operator
gives remainder of a divided by b]
·
Relational Operators
// Following Program uses relational
operators.
Line
1.
#include<iostream.h>
Line
2.
void main()
Line
3.
{
Line
4.
int a,b;
Line
5.
cout<< “Enter two Numbers”;
Line
6.
cin>>a>>b;
Line
7.
if(a<b)
Line
8.
{
Line
9.
cout<<” a is less than b”;
Line
10.
}
Line
11.
else if(a>b)
Line
12.
{
Line
13.
cout<<” a is greater than b”;
Line
14.
}
Line
15.
else if(a= =b)
Line
16.
{
Line
17.
cout<<” a and b are equal”;
Line
18.
}
Line
19.
}
Explanation: We can see that at Line
7,Line 11 and Line 15 there are conditions used using relational operators.
·
Logical
Operators
/* Following Program displays “Number
Even and less than 10”, “Number Even but not less than 10”, “Number Either Even
or less than 10 or both” Message depending on the value of variable “a”.*/
Line
1.
#include<iostream.h>
Line
2.
void main()
Line
3.
{
Line
4.
int a,b;
Line
5.
cout<< “Enter A Number”;
Line
6.
cin>>a;
Line
7.
if(a<10 && a%2==0)
Line
8.
{
Line
9.
cout<<”
Number Even and less than 10”;
Line
10.
}
Line
11.
else if(!(a<10) && a%2==0)
Line
12.
{
Line
13.
cout<<”
Number Even but not less than 10”;
Line
14.
}
Line
15.
else if(a<10 || a%2==0)
Line
16.
{
Line
17.
cout<<”
Number Either Even or less than 10 or both”;
Line
18.
}
Line
19.
}
Explanation: We can see that at Line
7,Line 11 and Line 15 there are conditions used using relational operators
combined with Logical Operators.
·
Difference between “=” and “= =”
operators
=
|
==
|
Arithmetic
Assignment Operator
|
Relational
Operator
|
It
assigns value from right to left. For Example int a = 5;
|
It checks
the value for equality. For Example if(a==5)
|
·
Conditional
Operator or Ternary Operator (? :)
If there are
three variables “a”, “b” and “c’ and we want “c” to store the higher value of “a”
or “b” , we can use the following code:
Line 1.
#include<iostream.h>
Line 2.
void
main()
Line 3.
{
Line 4.
int
a ,b, c;
Line 5.
cout<<”Enter
2 Numbers”;
Line 6.
cin>>a>>b;
Line 7.
if(a>b)
Line 8.
{
Line 9.
c=a;
Line 10. }
Line 11. else
Line 12. {
Line 13. c=b;
Line 14. }
Line 15. }
We required 15 lines of code. But we
can do the same thing in less number of lines using ternary or conditional
operator (? :)
Line 1.
#include<iostream.h>
Line 2.
void
main()
Line 3.
{
Line 4.
int
a ,b, c;
Line 5.
cout<<”Enter
2 Numbers”;
Line 6.
cin>>a>>b;
Line 7.
c=(a>b)?
a : b;
Line 8.
}
We saw that we have reduced nearly
half of the lines. At Line 7, we have used conditional operator which evaluates
a condition. If condition is true, variable before the colon (:) is assigned to
c otherwise if condition is false, variable after the colon(:) is assigned to
c.
Comments
Post a Comment