0% found this document useful (0 votes)
17 views29 pages

Data Type

The document provides an overview of data types in C++, including built-in, derived, and user-defined types. It explains fundamental data types such as integral, floating-point, and void, along with operators like arithmetic, relational, logical, and bitwise operators. Additionally, it covers the usage of structures, unions, classes, and enumerations as user-defined data types.

Uploaded by

Dilbagh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views29 pages

Data Type

The document provides an overview of data types in C++, including built-in, derived, and user-defined types. It explains fundamental data types such as integral, floating-point, and void, along with operators like arithmetic, relational, logical, and bitwise operators. Additionally, it covers the usage of structures, unions, classes, and enumerations as user-defined data types.

Uploaded by

Dilbagh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

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

Various user-defined data types provided by C++ are structures, unions,


enumerations and classes.
Structure, Union andClass: Structure and union are the significant features of C
language. Structure and union provide a way to group similar or dissimilar data types
referred to by a single name. However, C++ has extended the concept of structure and
union by incorporating some new features in these data types to support object -oriented
programming.
C++ offers a new user-defined data type known as class, which forms the basis of object-
oriented programming. A class acts as a template which defines the data and functions
that are included in an object of a class. Classes are declared using the keyword class.
Once a class has been declared, its object can be easily created.
Enumeration: An enumeration is a set of named integer constants that specify all the
permissible values that can be assigned to enumeration variables. These set of
permissible values are known as enumerators. For example, consider this statement.
enum country {US, UN, India, China}; // declaring an
// enum type
In this statement, an enumeration data-type country (country is a tag name) , consisting
of enumerators US, UN and so on, is declared. Note that these enumerators represent
By default, the first enumerator in the enumeration data type is assigned the value zero.
The value of subsequent enumerators is one greater than the value of previous
enumerator. Hence, the value of US is 0, value of UN is 1 and so on.

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.

Example of Arithmetic Operators


#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 = 40;
cout<<"num1 + num2: "<<(num1 + num2)<<endl;
cout<<"num1 - num2: "<<(num1 - num2)<<endl;
cout<<"num1 * num2: "<<(num1 * num2)<<endl;
cout<<"num1 / num2: "<<(num1 / num2)<<endl;
cout<<"num1 % num2: "<<(num1 % num2)<<endl;
return 0;
}
Output:

num1 + num2: 280


num1 - num2: 200
num1 * num2: 9600
num1 / num2: 6
num1 % num2: 0

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 right.

< returns true if left side is less than right side.

>= 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.

Example of Relational operators


#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 =40;
if (num1==num2) {
cout<<"num1 and num2 are equal"<<endl;
}
else{
cout<<"num1 and num2 are not equal"<<endl;
}
if( num1 != num2 ){
cout<<"num1 and num2 are not equal"<<endl;
}
else{
cout<<"num1 and num2 are equal"<<endl;
}
if( num1 > num2 ){
cout<<"num1 is greater than num2"<<endl;
}
else{
cout<<"num1 is not greater than num2"<<endl;
}
if( num1 >= num2 ){
cout<<"num1 is greater than or equal to num2"<<endl;
}
else{
cout<<"num1 is less than num2"<<endl;
}
if( num1 < num2 ){
cout<<"num1 is less than num2"<<endl;
}
else{
cout<<"num1 is not less than num2"<<endl;
}
if( num1 <= num2){
cout<<"num1 is less than or equal to num2"<<endl;
}
else{
cout<<"num1 is greater than num2"<<endl;
}
return 0;
}
Output:

num1 and num2 are not equal


num1 and num2 are not equal
num1 is greater than num2
num1 is greater than or equal to num2
num1 is not less than num2
num1 is greater than num2

Logical Operators: Logical Operators are used to combine two or more


conditions/constraints or to complement the evaluation of the original condition
in consideration. The result of the operation of a logical operator is a boolean
value either true or false. For example, the logical AND represented as ‘&&’
operator in C or C++ returns true when both the conditions under
consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns
true when both a and b are true (i.e. non-zero).
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: &&, ||, !

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.

Example of Logical Operators


#include <iostream>
using namespace std;
int main(){
bool b1 = true;
bool b2 = false;
cout<<"b1 && b2: "<<(b1&&b2)<<endl;
cout<<"b1 || b2: "<<(b1||b2)<<endl;
cout<<"!(b1 && b2): "<<!(b1&&b2);
return 0;
}
Output:

b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1

Assignment Operators: Assignment operators are used to assign value to a


variable. The left side operand of the assignment operator is a variable and the
right side operand of the assignment operator is a value. The value on the right
side must be of the same data type as the variable on the left side otherwise the
compiler will raise an error.
Different types of assignment operators are shown below:
Assignments operators in C++ are: =, +=, -=, *=, /=, %=

num2 = num1 would assign value of variable num1 to the variable.


num2+=num1 is equal to num2 = num2+num1

num2-=num1 is equal to num2 = num2-num1

num2*=num1 is equal to num2 = num2*num1

num2/=num1 is equal to num2 = num2/num1

num2%=num1 is equal to num2 = num2%num1

Example of Assignment Operators


#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 = 40;
num2 = num1;
cout<<"= Output: "<<num2<<endl;
num2 += num1;
cout<<"+= Output: "<<num2<<endl;
num2 -= num1;
cout<<"-= Output: "<<num2<<endl;
num2 *= num1;
cout<<"*= Output: "<<num2<<endl;
num2 /= num1;
cout<<"/= Output: "<<num2<<endl;
num2 %= num1;
cout<<"%= Output: "<<num2<<endl;
return 0;
}
Output:

= Output: 240
+= Output: 480
-= Output: 240
*= Output: 57600
/= Output: 240
%= Output: 0

Conditional operatotr: Conditional operator is of the form Expression1?


Expression2: Expression3. Here, Expression1 is the condition to be evaluated.
If the condition(Expression1) is True then we will execute and return the result
of Expression2 otherwise if the condition(Expression1) is false then we will
execute and return the result of Expression3. We may replace the use of if..else
statements with conditional operators.

Example of conditional Operator


#include <iostream>
using namespace std;
int main(){
int num1, num2; num1 = 99;
/* num1 is not equal to 10 that's why
* the second value after colon is assigned
* to the variable num2
*/
num2 = (num1 == 10) ? 100: 200;
cout<<"num2: "<<num2<<endl;
/* num1 is equal to 99 that's why
* the first value is assigned
* to the variable num2
*/
num2 = (num1 == 99) ? 100: 200;
cout<<"num2: "<<num2;
return 0;
}
Output:

num2: 200
num2: 100

Bitwise Operators: The Bitwise operators are used to perform bit-level


operations on the operands. The operators are first converted to bit-level and
then the calculation is performed on the operands. The mathematical
operations such as addition, subtraction, multiplication, etc. can be performed at
bit-level for faster processing. For example, the bitwise AND represented as &
operator in C or C++ takes two numbers as operands and does AND on every
bit of two numbers. The result of AND is 1 only if both bits are 1.

Operator Description

& Bitwise AND Operator

| Bitwise OR Operator

^ Bitwise XOR Operator


~ Bitwise Complement Operator

<< Bitwise Shift Left Operator

>> Bitwise Shift Right Operator

Example of Bitwise Operators


#include <iostream>
using namespace std;

main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;

c = a & b; // 12 = 0000 1100


cout << "Line 1 - Value of c is : " << c << endl ;

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 ;

c = ~a; // -61 = 1100 0011


cout << "Line 4 - Value of c is: " << c << endl ;

c = a << 2; // 240 = 1111 0000


cout << "Line 5 - Value of c is: " << c << endl ;

c = a >> 2; // 15 = 0000 1111


cout << "Line 6 - 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

++ and — num++ is equivalent to num=num+1;

num–- is equivalent to num=num-1;

Example of Auto-increment and Auto-decrement Operators


#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 = 40;
num1++; num2--;
cout<<"num1++ is: "<<num1<<endl;
cout<<"num2-- is: "<<num2;
return 0;
}
Output:

num1++ is: 241


num2-- is: 39

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.

Syntax of the sizeof() operator is given below:

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

Operator precedence determines which operator is performed first in an


expression with more than one operators with different precedence.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has
higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7
Operators Associativity The associativity of an operator is a property that
determines how operators of the same precedence are grouped in the absence
of parentheses. It is used when two operators of same precedence appear in an
expression. Associativity can be either Left to Right or Right to Left.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Category Operator Associativity
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right

*****************************************************************************************

C++ statements

C++ statements are executed sequentially, except when an expression statement, a


selection statement, an iteration statement, or a jump statement specifically modifies
that sequence.

Statements may be of the following types:


expression-statement
null-statement
compound-statement
selection-statement
iteration-statement
jump-statement
declaration-statement

 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.

Manipulator Purpose Header File


endl causes line feed to be inserted i.e. ‘\n’ iostream.h
dec,oct,hex set the desired number system iostream.h
setbase (b) output integers in base b iomanip.h
setw(w) read or write values to w characters iomanip.h
setfill (c) fills the whitespace with character c iomanip.h
setprecision(n) set floating point precision to n iomanip.h
Note: One should note that the manipulators endl, setw(), setfill() can be used with all
types of data, whereas manipulators setbase(), Dec, Oct, hex are used with integer data
types. Also, setprecision() manipulators used only with the float data types.

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

Input Output Statements in c++


cin and cout are two predefined objects which represent standard input and output
stream. The standard output stream represents the screen, while the standard input
stream represents the keyboard. These objects are members of iostream class. Hence
the header file <iostream.h> should be included in the beginning of all programs.
#include
void main() {
int i; //Declaration statement
cin>>i; //Input statement
cout<<i; //Output statement
}
Example:
Illustrates the use of cin and cout
The operator << is called the insertion operator and the operator >> is called the
extraction operator.
********************************************

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>

using namespace std;

int main() {

int time = 20;

if (time < 18) {

cout << "Good day.";

} else {

cout << "Good evening.";

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

// you can have any number of case statements.


default : //Optional
statement(s);
}

#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;
}

Output: You passed


Your grade is D

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

for(initialization; condition; incr/decr){


//code to be executed
}
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=10;i++){
cout<<i <<"\n";
}
}

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

Automatic Auto / memory Function Block block Unkown

Register Register / register Function Block block Unknown

External Extern / memory Whole Program program Zero

Static Static /memory Whole Program block Zero

Automatic Storage Class


It is the default storage class for all local variables. The auto keyword is applied to all local
variables automatically.

{
auto int y;
float y = 3.45;
}

Register Storage Class


The register variable allocates memory in register than RAM. Its size is same of register
size. It has a faster access than other variables.

It is recommended to use register variable only for quick access such as in counter.

Note: We can't get the address of register variable.

register int counter=0;

The static Storage Class


The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope. Therefore, making local variables static allows them
to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes
that variable's scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that
member to be shared by all objects of its class.

using namespace std;


void func() {
static int i=0; //static variable
int j=0; //local variable
i++;
j++;
cout<<"i=" << i<<" and j=" <<j<<endl;
}
int main()
{
func();
func();
func();
}
OUTPUT:
I =1 and j= 1
I =2 and j= 1
I =3 and j= 1

The extern Storage Class


The extern storage class is used to give a reference of a global variable that is visible to
ALL the program files. When you use 'extern' the variable cannot be initialized as all it
does is point the variable name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will be
used in other files also, then extern will be used in another file to give reference of defined
variable or function. Just for understanding extern is used to declare a global variable or
function in another file.
extern int counter=0;

You might also like