Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:32:25+00:00 2026-06-14T12:32:25+00:00

I’ve a bit confusion about static , auto , global and local variables. Somewhere

  • 0

I’ve a bit confusion about static, auto, global and local variables.

Somewhere I read that a static variable can only be accessed within the function, but they still exist (remain in the memory) after the function returns.

However, I also know that a local variable also does the same, so what is the difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:32:27+00:00Added an answer on June 14, 2026 at 12:32 pm

    There are two separate concepts here:

    • scope, which determines where a name can be accessed, and
    • storage duration, which determines when a variable is created and destroyed.

    Local variables (pedantically, variables with block scope) are only accessible within the block of code in which they are declared:

    void f() {
        int i;
        i = 1; // OK: in scope
    }
    void g() {
        i = 2; // Error: not in scope
    }
    

    Global variables (pedantically, variables with file scope (in C) or namespace scope (in C++)) are accessible at any point after their declaration:

    int i;
    void f() {
        i = 1; // OK: in scope
    }
    void g() {
        i = 2; // OK: still in scope
    }
    

    (In C++, the situation is more complicated since namespaces can be closed and reopened, and scopes other than the current one can be accessed, and names can also have class scope. But that’s getting very off-topic.)

    Automatic variables (pedantically, variables with automatic storage duration) are local variables whose lifetime ends when execution leaves their scope, and are recreated when the scope is reentered.

    for (int i = 0; i < 5; ++i) {
        int n = 0;
        printf("%d ", ++n);  // prints 1 1 1 1 1  - the previous value is lost
    }
    

    Static variables (pedantically, variables with static storage duration) have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.

    for (int i = 0; i < 5; ++i) {
        static int n = 0;
        printf("%d ", ++n);  // prints 1 2 3 4 5  - the value persists
    }
    

    Note that the static keyword has various meanings apart from static storage duration. On a global variable or function, it gives it internal linkage so that it’s not accessible from other translation units; on a C++ class member, it means there’s one instance per class rather than one per object. Also, in C++ the auto keyword no longer means automatic storage duration; it now means automatic type, deduced from the variable’s initialiser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I've read a lot about 64-bit versus 32-bit OS/Apps, but this question is specifically
I have read some web articles about Three-Tier Architecture. However, I am little bit
Here, I have a bit confusion about UI language. If language is changed then
Im a bit confused about inherited instance variables in ABCs. I have written an
Here's a bit of a newbie Python question about instance variables. Consider the following
A little bit confusion about some data structure in Python. Could any expert give
I have a PHP variable that looks a bit like this: $id = 01922312;
I have a PHP variable that looks a bit like this: $id = 01922312;
I have little bit confusion about barcode type support by zxing library in android
I have a little bit of confusion about iOS Apps publication. The first time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.