Computer Science

Storage Classes in C

Storage classes in C are used to define the scope, visibility, and lifetime of variables. There are four types of storage classes in C: automatic, static, register, and extern. Each storage class has its own set of rules and limitations that determine how variables are stored and accessed in memory.

Written by Perlego with AI-assistance

3 Key excerpts on "Storage Classes in C"

  • Book cover image for: ANSI C Programming
    eBook - ePub

    ANSI C Programming

    Learn ANSI C step by step

    We have not mentioned storage classes yet, though we have written several programs in C. We were able to get away with this because storage classes have defaults. If we don’t specify the storage class of a variable in its declaration, the compiler will assume a storage class depending on the context in which the variable is used. Thus, variables have certain default storage classes.
    From C compiler’s point of view, a variable name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept— Memory and CPU registers. It is the variable’s storage class that determines in which of these two types of locations, the value is stored.
    Moreover, a variable’s storage class tells us: (a) Where the variable would be stored. (b) What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value). (c) What is the scope of the variable; i.e. in which functions the value of the variable would be available. (d) What is the life of the variable; i.e. how long would the variable exist. There are four Storage Classes in C: (a) Automatic storage class (b) Register storage class (c) Static storage class (d) External storage class Let us examine these storage classes one by one.

    Automatic Storage Class

    The features of a variable defined to have an automatic storage class are as under:
    Storage - Memory.
    Default initial value - An unpredictable value, which is often called a garbage value.
    Scope - Local to the block in which the variable is defined.
    Life - Till the control remains within the block in which the variable is defined.
    Following program shows how an automatic storage class variable is declared, and the fact that if the variable is not initialized, it contains a garbage value. #include <stdio.h> int main() {   auto int i, j;   printf ("%d %d\n", i, j);   return 0; } The output of the above program could be… 1211 221
    where, 1211 and 221 are garbage values of i and j . When you run this program, you may get different values, since garbage values are unpredictable. So always make it a point that you initialize the automatic variables properly, otherwise you are likely to get unexpected results. Note that the keyword for this storage class is auto
  • Book cover image for: Introduction to Programming with C++ for Engineers
    349 Introduction to Programming with C++ for Engineers, First Edition. Bogusław Cyganek. © 2021 John Wiley & Sons Ltd. Published 2021 by John Wiley & Sons Ltd. Companion website: http://home.agh.edu.pl/~cyganek/BookCpp.htm All objects used in computations must be placed somewhere in computer memory. We touched on memory management when discussing variables, as well as arrays and pointers (Section 3.1). The two most important concepts related to objects and memory are these: ■ Object visibility (scope) – The rules telling which parts of the code an object can be accessed from. The scope of an object that is not a member of a class starts at a point of its declaration. ■ Object lifetime – How long an object is “alive” in memory, and which components are responsible for its creation and deletion. Knowing these is essential for proper code organization. However, although object visibility is an easy concept, understanding object lifetime and responsibility for an object’s creation and deletion is much more demanding. Violating these guidelines can result in serious programming errors, such as accessing deleted objects, and in memory leaks. These problems, as well as methods to remedy them, are discussed in this chapter. 5.1 Types of Data Storage C++ programs use various data structures to store different types of objects (variables and con- stants); see Appendix A.3 Also, depending on the storage type, there are different rules for object creation and deletion. These are discussed in Table 5.1. 5.2 Dynamic Memory Allocation – How to Avoid Memory Leaks As alluded to previously, the C++ language has two underground data structures for storing objects: the stack, associated with each block of a function or a statement; and the heap, used for dynamic memory allocation. The stack is created when program execution steps into a function or statement and is automatically destroyed, along with all of its contents, after execution steps out of it.
  • Book cover image for: Ivor Horton's Beginning Visual C++ 2013
    • Ivor Horton(Author)
    • 2014(Publication Date)
    • Wrox
      (Publisher)
    UNDERSTANDING STORAGE DURATION AND SCOPE All variables have a finite lifetime. They come into existence at the point at which you declare them and then, at some point, they disappear — at the latest, when your program terminates. How long a particular variable lasts is determined by a property called its storage duration. There are three dif-ferent kinds of storage duration that a variable can have: ➤ Automatic storage duration ➤ Static storage duration ➤ Dynamic storage duration Which of these a variable will have depends on how you create it. I will defer discussion of variables with dynamic storage duration until Chapter 4, but you will be exploring the characteristics of the other two in this chapter. Understanding Storage Duration and Scope ❘ 73 Another property that variables have is scope . The scope of a variable is that part of your program over which the variable name is valid. Within a variable’s scope, you can legally refer to it, either to set its value or to use it in an expression. Outside of the scope of a variable, you cannot refer to its name — any attempt to do so will cause a compiler error. Note that a variable may still exist outside of its scope, even though you cannot refer to it. You will see examples of this a little later in this discussion. All the variables that you have declared up to now have had automatic storage duration , and are therefore called automatic variables . Let’s take a closer look at these first. Automatic Variables The variables that you have declared so far have been declared within a block — that is, within the extent of a pair of braces. These are automatic variables and have local scope or block scope . An automatic variable is “in scope” from the point at which it is declared until the end of the block con-taining its declaration. The space that an automatic variable occupies is allocated in a memory area called the stack that is set aside specifically for this purpose.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.