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

  • Home
  • SEARCH
  • 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 942309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:15:05+00:00 2026-05-15T22:15:05+00:00

I have a confusion in the concepts of static integer.When i initialize a static

  • 0

I have a confusion in the concepts of static integer.When i initialize a static integer in main function i.e

static int i;

Now the static integer is assigned a value 0.Now in the next step:

i++;

i becomes 1.

Now the program terminates. I want to know what program will produce on its’ next run. Also, what would happen if the entire program is closed? I understand that the first line is static int i; thus the next value when the function is next run should retain the value of i when it was previously run. If so, what is the advantage of making a variable static? Does the variable have a time limit or can it be stored forever? What would the value be if I ran the function again?

  • 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-05-15T22:15:06+00:00Added an answer on May 15, 2026 at 10:15 pm

    In C, “static” means that the variable has local scope within global storage.

    The scope of variables in C is a block. In other words variables can be used inside the block they are declared. And normally they just keep their values until the block ends, being lost after that. Example:

    {
       int a; 
       // Only a can be used here
       {
          int b;
          // a and b can be used here
          {
             int c;
             // a,b and c can be used here
          }
          //just a and b can be used here. c is not available anymore
        }
        // only a can be used here. Neither b nor c are available anymore
     }
    

    This is true except for global variables that can be used all along the program.

    The other exception is the static variable. It is only seen inside the block but keeps its value after the block is over.

    This means that if you declare a static variable inside a function, it will maintain its value between function calls.

    For example, the function below has a local variable. Local variables have scope of block (this means you can only access the variable ‘var’ inside the block {} it is declared, in the case below inside the function):

    void countFunction(void)
    {
      int var = 0;
      var = var + 1;
      printf("Value is %d\n", var);
    }
    

    Once the variable is not static, every time you call the function it will print “Value is 1” because the variable is stored in the stack that is allocated on the function call and deallocated after the function returns.

    If you change var to be static,

    void countFunction(void)
    {
      static int var = 0;
      var = var + 1;
      printf("Value is %d\n", var);
    }
    

    The first time you call the function var will be initialized as 0 and the function will show “Value is 1”. Nevertheless, at the second time, var will be already allocated and at a global area. It will not be initialized again and the function will display “Value is 2”.

    This within the program execution.

    Although a static variable is allocated as long as your program executes, it does not keep its value after your program finishes (the program will free all of its memory). The only way to keep any value for the next run is to store it at a non-volatile media like the disk.

    Hope it helps.

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

Sidebar

Related Questions

If I have a class called Test :: class Test { static std::vector<int> staticVector;
I have confusion about GridView in ASP.NET. How does the GridView exactly work? I
I have some confusion related to the .NET platform build options in Visual Studio
mmm, I have just a little confusion about multiple auto declarations in the upcoming
I have two basic interface-related concepts that I need to have a better understanding
I have started design of a ColdFusion application that is entirely web based. Not
I have been tasked with going through a number of ColdFusion sites that have
I have a servlet that I would like to run within ColdFusion MX 7.
I have been starting to write some reasonably large and or confusing MySQL queries
I have a web application, written in ColdFusion, which periodically starts using 100% of

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.