• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » Data Storage Type in C++
Next →
← Prev

Data Storage Type in C++

By Dinesh Thakur

Auto

The values of the variables are not retained beyond the scope of a function in which they are declared. The values of the variables can be accessed only during the execution of a function. Whatever variables are declared in a function, they are assumed implicitly of auto storage type. For example in the following program segment.

#include 
void f1() {
  int v3, v4;
  . .
}
void Main() {
   int v1, v2;
   f1();
}

The variable declaration

  int v1, v2;

in the Main() function implicitly means

  auto int v1, v2;

and the variable declaration

  int v3, v4;

in the f1() function implicitly means

  auto int v3, v4;

The variables v1, v2 are local to the Main() function and the variables v3, v4 are local to the no function. If the variables are to be retained beyond the scope of a function, static storage type has to be used.

Static

In the case of static type of data, values of the variables declared in a function are retained even after the execution of the function.

Example

The program illustrates auto storage type.

#include 
void st();
void main() {
  int v1;
  for(v1=0;v1<4;v1++) {
     st();
  }
}
void st() {
    auto int sum = 0;
    int i;
    cout<<"\ninput the value of i ";
    cin>>i;
    sum=sum+i;
    cout<<"The value of sum is ";
    cout<<sum;
}

Input and Output:

input the value of i 1

The value of sum is 1

input the value of i 3

The value of sum is 3

input the value of i 5

The value of sum is 5

input the value of i 8

The value of sum is 8

Example

The program illustrates the use of static storage type.

II static storage type

#include 
void st();
void main() {
  int v1;
  for(v1 =0;v1<4;v1++) {
      st();
  }
}
   void st() {
      static int sum = 0;
      int i;
      cout<<"\ninput the value of i ";
      cin>>i;
      sum=sum+i;
      cout<<"The value of sum is ";
      cout<<sum;
}

Input and Output:

input the value of i 1

The value of sum is 1

input the value of i 3

The value of sum is 4

input the value of i 5

The value of sum is 9

input the value of i 8

The value of sum is 17

Example

Illustrates another program using static storage type.

#include 
void sum(int a) {
  static int sum = 0;
  static int count = 0;
  sum = sum +a;
  cout<<"count "<<++count;
  cout<<'\n';
  cout<<"Total sum "<<sum;
  cout<<'\n';
}
void main() {
   char policyno[10];
   char custname[10];
   int amount;
   char ch;
   do {
      cout<<"Enter policy number";
      cin>>policyno;
      cout<<"Enter customer name";
      cin>>custname;
      cout<<"Enter amount";
      cin>>amount;
      sum(amount);
      cout<<"\nDo you want to insert more information. Type y or n. ";
      cin>>ch;
}
while(ch=='y');
}

Input and Output:

Enter policy number P197

Enter customer name Rajesh

Enter amount 1000

count 1

Total sum 1000

Do you want to insert more information. Type y or n. y

Enter policy number P297

Enter customer name Kamal

Enter amount 1200 ,

count 2

Total sum 2200

Do you want to insert more information. Type y or n. n

In this program, sum and count are of static storage type. Every time when the information for a new customer is entered, count is incremented by 1 and the amount for which customer has insured is added to the previous value of sum.

Extern

This type of data does not allocate any memory for a variable but it is just declared. A variable declared as extern type must be used to access a global variable declared in another program file. It must also be used to access a global variable later in the same program.

Example

Illustrates the use of extern storage type.

#include 
void a();
int vl,v2;
void main() {
  cout<<"Input the value of vi and v2";
  cin>>vl>>v2;
  a();
}
void a() {
  extern int vi;
  extern int v2;
  cout<<"Display the value of vi * v2 is ";
  cout<

You’ll also like:

  1. Data Types – Explain Data Type in C++.
  2. Abstract Data Type – What is an Abstract Data Type (ADT)?
  3. Enumeration Data Type
  4. Explain C# Data Type
  5. Write a C program to define new data type.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW