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 128151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:35:23+00:00 2026-05-11T05:35:23+00:00

In C you can have external static variables that are viewable every where in

  • 0

In C you can have external static variables that are viewable every where in the file, while internal static variables are only visible in the function but is persistent

For example:

#include <stdio.h>  void foo_bar( void ) {         static counter = 0;         printf('counter is %d\n', counter);         counter++; } int main( void ) {         foo_bar();         foo_bar();         foo_bar();  return 0; } 

the output will be

counter is 0 counter is 1 counter is 2 

My question is why would you use an internal static variable? If you don’t want your static variable visible in the rest of the file shouldn’t the function really be in its own file then?

  • 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. 2026-05-11T05:35:24+00:00Added an answer on May 11, 2026 at 5:35 am

    This confusion usually comes about because the static keyword serves two purposes.

    When used at file level, it controls the visibility of its object outside the compilation unit, not the duration of the object (visibility and duration are layman’s terms I use during educational sessions, the ISO standard uses different terms which you may want to learn eventually, but I’ve found they confuse most beginning students).

    Objects created at file level already have their duration decided by virtue of the fact that they’re at file level. The static keyword then just makes them invisible to the linker.

    When used inside functions, it controls duration, not visibility. Visibility is already decided since it’s inside the function – it can’t be seen outside the function. The static keyword in this case, causes the object to be created at the same time as file level objects.

    Note that, technically, a function level static may not necessarily come into existence until the function is first called (and that may make sense for C++ with its constructors) but every C implementation I’ve ever used creates its function level statics at the same time as file level objects.

    Also, whilst I’m using the word ‘object’, I don’t mean it in the sense of C++ objects (since this is a C question). It’s just because static can apply to variables or functions at file level and I need an all-encompassing word to describe that.

    Function level statics are still used quite a bit – they can cause trouble in multi-threaded programs if that’s not catered for but, provided you know what you’re doing (or you’re not threading), they’re the best way to preserve state across multiple function calls while still providing for encapsulation.

    Even with threading, there are tricks you can do in the function (such as allocation of thread specific data within the function) to make it workable without exposing the function internals unnecessarily.

    The only other choices I can think of are global variables and passing a ‘state variable’ to the function each time.

    In both these cases, you expose the inner workings of the function to its clients and make the function dependent on the good behavior of the client (always a risky assumption).

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

Sidebar

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.