Should "static" variable always be local to a file?

This is a discussion on Should "static" variable always be local to a file? within the C++ Programming forums, part of the General Programming Boards category; Here is the code: Code: //file main.cc #include "test.h" int main(){ cout<<x; } Code: //file test.h static int x = ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Should "static" variable always be local to a file?

    Here is the code:

    Code:
    //file main.cc
    
    #include "test.h"
    
    int main(){
     cout<<x;
    }
    Code:
    //file test.h
    
    static int x = 10;

    This code works. But should static int x be local to test.h only? How come it can be used in a totally different file?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Do not define variables in header files. Define them in source files, ie .cpp files instead.
    Yes, if you define a global variable with the "static" keyword, it means it will only be visible in the current source file. This can allow for some more compiler optimizations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    It can be used in a totally different file, but then it would be a distinct variable. So for example &x will not be the same, and if you change x in one file, it will not change in the other.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I see this is duplicate question for the same thing.
    The answer is, just as in the other thread: header files are not compiled. Their contents are pasted into source files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21