Data Type
Data Type
A data type is a classification of data which tells the compiler or interpreter how
the programmer intends to use the data. C++ provides various data types and
each data type is represented differently within the computer’s memory. The various data
types provided by C++ are built-in data types, derived data types and user-defined data
types as shown in Figure.
Built-inDataTypes
The basic (fundamental) data types provided by c++ are integral, floating
point and void data type. Among these data types, the integral and floating-point data
types can be preceded by several type modifiers. These modifiers (also known as type
qualifiers) are the keywords that alter either size or range or both of the data types.
The various modifiers are short, long, signed and unsigned. By default the modifier is
signed.
In addition to these basic data types, ANSI C++ has introduced two more data types
namely, bool and wchar_t.
Integral Data Type: The integral data type is used to store integers and includes char
(character) and int (integer) data types.
Char: Characters refer to the alphabet, numbers and other characters (such as {, @, #,
etc.) defined in the ASCII character set. In C++, the char data type is also treated as an
integer data type as the characters are internally stored as integers that range in value
from -128 to 127. The char data type occupies 1 byte of memory (that is, it holds only
one character at a time).
The modifiers that can precede char are signed and unsigned. The various character
data types with their size and range are listed in Table
The modifiers that can precede char are signed and unsigned. The various character
data types with their size and range are listed in Table
Int: Numbers without the fractional part represent integer data. In C++, the int data type
is used to store integers such as 4, 42, 5233, -32, -745. Thus, it cannot store numbers
such as 4.28, -62.533. The various integer data types with their size and range are listed
in Table
Floating-point Data Type: A floating-point data type is used to store real numbers such
as 3 .28, 64. 755765, 8.01, -24.53. This data type includes float and double’ data types.
The various floating -point data types with their size and range are listed in Table
Void: The void data type is used for specifying an empty parameter list to a function and
return type for a function. When void is used to specify an empty parameter list, it
indicates that a function does not take any arguments and when it is used as a return
type for a function, it indicates that a function does not return any value. For void, no
memory is allocated and hence, it cannot store anything. As a result, void cannot be
used to declare simple variables, however, it can be used to declare generic pointers.
Bool and wcha_t : The boo1data type can hold only Boolean values, that is; either true
or false, where true represents 1 and false represents O. It requires only one bit of
storage, however, it is stored as an integer in the memory. Thus, it is also considered as
an integral data type. The bool data type is most commonly used for expressing the
results of logical operations performed on the data. It is also used as a return type of a
function indicating the success or the failure of the function.
In addition to char data type, C++ provides another data type wchar_t which is used to
store 16- bit wide characters. Wide characters are used to hold large character sets
associated with some non-English languages.
Derived Data Types: Data types that are derived from the built-in data types are known
as derived data types. The various derived data types provided by C++ are arrays,
junctions, references and pointers.
Array An array is a set of elements of the same data type that are referred to by the
same name. All the elements in an array are stored at contiguous (one after another)
memory locations and each element is accessed by a unique index or subscript value.
The subscript value indicates the position of an element in an array.
Function A function is a self-contained program segment that carries out a specific well-
defined task. In C++, every program contains one or more functions which can be
invoked from other parts of a program, if required.
Reference A reference is an alternative name for a variable. That is, a reference is an
alias for a variable in a program. A variable and its reference can be used
interchangeably in a program as both refer to the same memory location. Hence,
changes made to any of them (say, a variable) are reflected in the other (on a reference).
Pointer A pointer is a variable that can store the memory address of another variable.
Pointers allow to use the memory dynamically. That is, with the help of pointers, memory
can be allocated or de-allocated to the variables at run-time, thus, making a program
more efficient.
User-Defined Data Types
OPERATORS
Operators are the foundation of any programming language. Thus the
functionality of the C/C++ programming language is incomplete without the use
of operators. We can define operators as symbols that help us to perform
specific mathematical and logical computations on operands. In other words,
we can say that an operator operates the operands.
For example, consider the below statement:
c = a + b;
Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands
‘a’ and ‘b’.
Arithmetic Operators: These are the operators used to perform
arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–).
Basic arithmetic operators are: +, -, *, /, %
+ is for addition.
– is for subtraction.
* is for multiplication.
/ is for division.
% is for modulo.
Relational Operators: These are used for the comparison of the values of two
operands. For example, checking if one operand is equal to the other operand
or not, an operand is greater than the other operand or not, etc. Some of the
relational operators are (==, >= , <= )
We have six relational operators in C++: ==, !=, >, <, >=, <=
== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of operator.
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
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.
b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1
= Output: 240
+= Output: 480
-= Output: 240
*= Output: 57600
/= Output: 240
%= Output: 0
num2: 200
num2: 100
Operator Description
| Bitwise OR Operator
main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;
return 0;
}
Output:
Line 1 - Value of c is : 12
Line 2 - Value of c is: 61
Line 3 - Value of c is: 49
Line 4 - Value of c is: -61
Line 5 - Value of c is: 240
Line 6 - Value of c is: 15
Auto-increment and Auto-decrement Operator
Special operators
sizeof operator:The sizeof() is an operator that evaluates the size of data type,
constants, variable. It is a compile-time operator as it returns the size of any variable or a
constant at the compilation time.
The size, which is calculated by the sizeof() operator, is the amount of RAM occupied in
the computer.
sizeof(data_type);
In the above syntax, the data_type can be the data type of the data, variables, constants, unions,
structures, or any other user-defined data type.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. // Determining the space in bytes occupied by each data type.
6. std::cout << "Size of integer data type : " <<sizeof(int)<< std::endl;
7. std::cout << "Size of float data type : " <<sizeof(float)<< std::endl;
8. std::cout << "Size of double data type : " <<sizeof(double)<< std::endl;
9. std::cout << "Size of char data type : " <<sizeof(char)<< std::endl;
10. return 0;
11. }
output
*****************************************************************************************
C++ statements
Expression statements. These statements evaluate an expression for its side effects or
for its return value.
Null statements. These statements can be provided where a statement is required by
the C++ syntax but where no action is to be taken.
Compound statements. These statements are groups of statements enclosed in curly
braces ({ }). They can be used wherever a single statement may be used.
Selection statements. These statements perform a test; they then execute one section
of code if the test evaluates to true (nonzero). They may execute another section of
code if the test evaluates to false.
Iteration statements. These statements provide for repeated execution of a block of
code until a specified termination criterion is met.
Jump statements. These statements either transfer control immediately to another
location in the function or return control from the function.
Declaration statements. Declarations introduce a name into a program.
MANIPULATORS
The unformatted I/O statements that it is impossible to display output in a required user
format or input the values in the desired form. In certain situations, we may need to
format the I/O as per user requirements. For example :
1) The square root of a number should be displayed upto 2 decimal places.
2) The number to be inputted must be in a hexadecimal form.
To overcome the problems of unformatted I/O operations in C++, the concept of
manipulators was introduced.
The manipulators in C++ are special functions that can be used with insertion (<<) and
extraction (>>) operators to manipulate or format the data in the desired way. Certain
manipulators are used with << operator to display the output in a particular format,
whereas certain manipulators are used with >> operator to input the data in the desired
form. The manipulators are used to set field widths, set precision, inserting a new line,
skipping white space etc. In a single I/O statement, we can have more than one
manipulator, which can be chained as shown
1 cout<<manipl<<var1<<manip2<<var2;
2 cout<<manipl<<manip2<<var1;
To use most of the manipulators, we need to include the header file iomanip.h in the
program.
endl Manipulator
The endl manipulator stands for endline and is used with an insertion operator (<<) that
moves the cursor to the next line. If we do not use endl, the next output will be displayed
in the same line. The endl has the same function as that of ‘\n.’
#include<iostraam.h>
#include<conio.h>
int main() {
cout<<"Entar name"<<endl;
cout<<"Myname is Thakur";
qetch();
return 0;
Output
Enter name
Myname is Thakur
CONDITIONAL STATEMENTS
Conditional statements, also known as selection statements, are used to make
decisions based on a given condition. If the condition evaluates to True, a set of
statements is executed, otherwise another set of statements is executed.
The if Statement: The if statement selects and executes the statement(s) based on a
given condition. If the condition evaluates to True then a given set of statement(s) is
executed. However, if the condition evaluates to False, then the given set of statements
is skipped and the program control passes to the statement following the if statement.
The syntax of the if statement is
if (condition) {
// block of code to be executed if the condition is true
}
#include <iostream>
using namespace std;
int main() {
if (20 > 18) {
cout << "20 is greater than 18";
}
return 0;
}
Output:x is greater than y.
The if-else Statement: The if – else statement causes one of the two possible
statement( s) to execute, depending upon the outcome of the condition.
The syntax of the if-else statements is
if(condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
Here, the if-else statement comprises two parts, namely, if and else. If the condition is
True the if part is executed. However, if the condition is False, the else part is executed.
#include <iostream>
int main() {
} else {
return 0;
}
Output:good evening
Nested if-else Statement: A nested if-else statement contains one or more if-else
statements
The syntax is
if(condition1) {
statement1;
if (condition2)
statement2;
else
statement3;
}
else {
statement4;
if (condition3)
statement5;
else
statement6;
}
statement7;
if (a>b) {
if (a>c)
cout<<"a is largest";
}
else //nested if-else statement within else {
if (b>c)
cout<<"b is largest";
else
cout<<"c is largest";
}
Switch statements: A switch statement allows a variable to be tested for equality against a
list of values. Each value is called a case, and the variable being switched on is checked for
each case.
Syntax
The syntax for a switch statement in C++ is as follows −
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
char grade = 'D';
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
ITERATION STATEMENTS
The statements that cause a set of statements to be executed repeatedly either for a specific
number of times or until some condition is satisfied are known as iteration statements. That
is, as long as the condition evaluates to True, the set of statement(s) is executed. The
various iteration statements used in C++ are for loop, while loop and do while loop.
The for Loop
The for loop is one of the most widely used loops in C++. The for loop is a deterministic loop
in nature, that is, the number of times the body of the loop is executed is known in advance.
Flowchart:
The syntax of the for loop is
Output: 1 to 10
WHILE LOOP:
The while loop is used to perform looping operations in situations where the number of
iterations is not known in advance. That is, unlike the for loop, the while loop is non
deterministic in nature.
The syntax of the while loop is
while(condition) {
// body of while loop
}
Flowchart:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i=1;
5. while(i<=10)
6. {
7. cout<<i <<"\n";
8. i++;
9. }
10. }
11. OUTPUT: 1 to 10
12.
DO WHILE LOOP
The C++ do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
The C++ do-while loop is executed at least once because condition is checked after loop
body.
do{
//code to be executed
}while(condition);
Flowchart:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do{
cout<<i<<"\n";
i++;
} while (i <= 10) ;
}
OUTPUT: 1 to 10
1) Break Statement.
This statement is used for stopping the execution of the program the break statement is
used where we do not want to execute the next statements Generally we can use a
Break Statement when we wants to Terminate a Program Execution The Statements
those are Placed after the Break are never to be Executed. Break Statement can be use
either in Break and Loops etc
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
return 0;
}.
OUTPUT: 0 1 2 3
2) Continue statement
The Continue Statements is used for executing the statements by skipping a
statement for eg if u don t want to execute any statement then we can use the continue
means to skip a particular statement and then execute the next statements.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
Return0;
}
OUTPUT: 0 to 9
3) Goto statement
The Goto Statement is used for transferring the control to the other location for eg if you
wants to transfer the control to the other place of the program then we can use the goto
statement. When we use goto Statement then we have to supply a name along with a
Goto Statement on which Control is transferred. The Name is user choice and Name
must be defined with the help of a Colon. When Compiler transfers the Control of
program to the Goto statement then statements of Goto block will be Executed and The
Name of Goto Statements block is also called as the Label Name.
************************************************************************************
STORAGE CLASS
A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C++ Program. These specifiers precede the type that they modify. There are
following storage classes, which can be used in a C++ Program
auto
register
static
extern
Storage Class Keyword /storage Lifetime Visibility Initial Value
memory
{
auto int y;
float y = 3.45;
}
It is recommended to use register variable only for quick access such as in counter.