Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
2.1k views
in C++ by (178k points)
What is the difference between automatic and static variables?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

The main difference between automatic and static variables in C++ lies in their storage duration and scope.

  1. Automatic Variables:

    • Storage Duration: Automatic variables have automatic storage duration, which means they are created when the program execution enters the scope where they are defined and destroyed when the execution leaves that scope.
    • Scope: Automatic variables are limited to the block or function in which they are defined. They are typically local variables declared within a function.
    • Initialization: Automatic variables are not automatically initialized with a value. Their initial value is undefined unless explicitly assigned a value.
    • Example:
      void myFunction() {
          int x;  // Automatic variable
          // ...
      }
       
  2. Static Variables:

    • Storage Duration: Static variables have static storage duration, which means they are created and initialized when the program starts and persist throughout the entire program execution.
    • Scope: Static variables have a scope that is local to the block or function in which they are defined. However, they retain their value between multiple function calls.
    • Initialization: Static variables are automatically initialized with the default value corresponding to their type if no explicit initialization is provided.
    • Example:
      void myFunction() {
          static int count = 0;  // Static variable
          // ...
          count++;
          // ...
      }
       

The key differences can be summarized as follows:

  • Automatic variables are created and destroyed as the program enters and leaves their scope, respectively, while static variables persist throughout the program execution.
  • Automatic variables are typically local to a specific block or function, while static variables are local to a block or function but retain their value across multiple function calls.
  • Automatic variables are not automatically initialized, whereas static variables are automatically initialized with default values if not explicitly initialized.

It's worth noting that static variables can also have file scope, which means they are accessible across multiple translation units (source files) in a program. However, this goes beyond the scope of comparing automatic and static variables within a single function or block.

I hope this clarifies the distinction between automatic and static variables in C++. Let me know if you have any further questions!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 27, 2023 in C++ by kvdevika (178k points)
0 votes
2 answers
asked May 27, 2023 in C++ by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...