Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Storage Classes

By Sripriya R | on September 10, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about storage classes, types of storage class variables – Automatic, External and Static explained with examples.

Storage classes:

In the context of scope of variables in functions exists the important concept of storage class.

What is Storage Class?

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program.

Types of Storage Class Variables in C++:

  • Automatic
  • External
  • Static

Automatic:

Variables defined within the function body are called automatic variables. Auto is the keyword used to declare automatic variables. By default and without the use of a keyword, the variables defined inside a function are automatic variables.

For instance:

  1. void exforsys( )
  2. { 
  3. auto int x;
  4. auto float y;
  5. ...
  6. ...
  7. }

is the same as

  1. void exforsys( )
  2. { 
  3. int x;
  4. float y;              //Automatic Variables
  5. ...
  6. ...
  7. }

In the above function, the variable x and y are created only when the function exforsys( ) is called. An automatic variable is created only when the function is called. When the function exforsys( ) is called, the variables x and y are allocated memory automatically. When the function exforsys( ) is finished and exits the control transfers to the calling program, the memory allocated for x and y is automatically destroyed. The term automatic variable is used to define the process of memory being allocated and automatically destroyed when a function is called and returned. The scope of the automatic variables is only within the function block within which it is defined. Automatic variable are also called local variables.

External:

External variables are also called global variables. External variables are defined outside any function, memory is set aside once it has been declared and remains until the end of the program. These variables are accessible by any function. This is mainly utilized when a programmer wants to make use of a variable and access the variable among different function calls.

Static:

The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

For example:

  1.  #include <iostream>
  2. using namespace std;
  3. int exforsys(int);
  4. int exforsys2(int);
  5. void main( )
  6. {
  7. 	int in;
  8. 	int out;
  9. 	while(1) 
  10. 	{
  11. 		cout << "nEnter input value:";
  12. 		cin>>in;
  13. 		if (in == 0) 
  14. 			break;
  15.  
  16. 		out=exforsys(in);
  17. 		cout << "nResult : " << out;
  18.  
  19. 		out=exforsys2(in);
  20. 		cout << "nResult2: " << out;
  21. 	} 
  22. 	cout << "n End of Program " ;
  23. }
  24.  
  25. int exforsys(int x)
  26. {
  27. 	static int a=0;
  28. 	a++;
  29. 	return(a);
  30. } 
  31. int exforsys2(int x)
  32. {
  33. 	int b=0;
  34. 	b++;
  35. 	return(b);
  36. }

In the above program, the static variables a is initialized only once in the beginning of the program. Then the value of the variable is maintained between function calls.

When the program begins, the value of static variable a is initialized to zero. The value of the input in is 3 (which is not equal to zero) and is then passed to the function in variable x. The variable a is incremented thus making a as equal to 1, thus, the return of value from function exforsys( ) for the first time is 1, which is printed in the called function.

It is the same case for the second function, exforsys2(), the variable b which is not declared static, is incremented thus making it equal to 1, and the return value for the first time is 1.

The second time the value of the input in is 7 (which is not equal to zero) and is passed to the function in variable x. The variable a (which is declared as static) has the previous value of 1. This is incremented and the value of a is equal to 2, so the return value is now 2.

Now, for the second function, the variable b which was not declared static, starts from 0 again, making it equal to 1 after incrementing it. Being not declared static, it will always start from 0.

Thus the output of the above program is:

« « C++ Operator Overloading Part II
ERP Impact in Organizations » »

Author Description

Avatar

Free Training

RSSSubscribe 401 Followers
  • Popular
  • Recent
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Encapsulation

    August 16, 2007 - 0 Comment
  • C++ Operators Part II

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Polymorphism

    August 16, 2007 - 0 Comment
  • C++ Manipulators

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part II

    September 9, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Decision Making Statements

    September 1, 2007 - 0 Comment
  • C++ Functions

    September 21, 2006 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled