Thread: I do not understand static global variable in C

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    14

    I do not understand static global variable in C

    Hello Everybody

    I found the description for the keyword static here Static Variables in C - GeeksforGeeks which I do not understand for static global variable

    Any help would be appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    We have a recent thread that you might want to read: global versus static variables
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by laserlight View Post
    We have a recent thread that you might want to read: global versus static variables
    It say when static use inside in function it is only visible only to the function in which it is defined. and when static use outside the function it say variable will visible to all the functions in the current source file

    It means there is no difference between auto global variable (declare outside of function) and static global variable (declare outside of function) because both are visible to all the function in the current source file and both variable will alive until program is running

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you only have one source file, sure. Most non-trivial programs consist of many source files so the difference does matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    7
    This may not be accurate, but my understanding is that marking a global variable in a source file as static means that while the variable is indeed global, it's global only to the functions in that particular source file and isn't visible to other files. One use for this could be sharing global state among a limited subset of functions as part of a program.

    So, think of the keyword "static" - at least as it applies to global variables - as meaning "this source file only".
    Last edited by TheMuffenMann; 04-17-2020 at 11:50 AM.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by TheMuffenMann View Post
    This may not be accurate, but my understanding is that marking a global variable in a source file as static means that while the variable is indeed global, it's global only to the functions in that particular source file and isn't visible to other files. One use for this could be sharing global state among a limited subset of functions as part of a program.

    So, think of the keyword "static" - at least as it applies to global variables - as meaning "this source file only".
    When we refer to a "global" (Non-static) variable, it is a variable that will be visible to any function in any file as long as there is an "extern" declaration, usually in a header file. The variable symbol is passed to the linker to be resolved by the linker. You would not declare a variable in a header file as both "extern" and "static"!

    A "static" variable defined outside of any function in one .c file, is visible to any function defined after the definition of the "static" variable in that one .c file. The symbol is not passed to the linker and all references to the static variable are resolved by the compiler.

    I would avoid calling a file level static variable, global!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2020, 04:27 AM
  2. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

Tags for this Thread