Thread: variable in file scope ???

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    variable in file scope ???

    how to make a variable in file scope.

    I mean
    it should not be local to a function.
    and it should not be global ( available to other source files).

    it should be availble only in the current source file.
    Any function in that file should be able to access that variable.
    and it should not be available to other source files.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use the 'static' keyword
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Thanks :-)

    Thanks Salem :-)

    Actually i knew abt this fact of static.
    but still , i was not sure.
    and i just wanted to confirm.

    soooo....

    thanks again.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Another option is to use an unnamed namespace.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yeah this is c++ and that use of static is now deprecated. An anonymous namespace is what has taken over this use of static.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Ken Fitlike says
    Another option is to use an unnamed namespace.


    well,
    this is my code, it is working fine with static.
    but i would like to know how to do it using unnamed namespace.




    //// mainfile.cpp
    Code:
    #include<iostream.h>
    void ff();
    void ff2();
    
    void main()
    {
    	cout<<" inside the main\n";
    	//i=200;
    	//cout<<"\n value of i is "<<i<<"\n";
    
    	ff();
    	ff2();
    
    }
    //// secondfile.cpp
    Code:
    #include<iostream.h>
    static int i=100;
    void ff()
    {
    	cout<<"\n inside the ff() \n ";
    	i=500;
    	cout<<"\n value of i is "<<i<<"\n";
    
    }
    
    void ff2()
    {
    	cout<<"\n inside the ff2() \n ";
    	i=600;
    	cout<<"\n value of i is "<<i<<"\n";
    
    }




    ************************
    i got it




    //// secondfile.cpp
    Code:
    #include<iostream.h>
    namespace {
    int i=100;
    }
    void ff()
    {
    	cout<<"\n inside the ff() \n ";
    	i=500;
    	cout<<"\n value of i is "<<i<<"\n";
    
    }
    
    void ff2()
    {
    	cout<<"\n inside the ff2() \n ";
    	i=600;
    	cout<<"\n value of i is "<<i<<"\n";
    
    }

    This works fine,
    but i wonder how variable i is accessible in the function ff() & ff2() in the file secondfile.cpp

    ????????????




    *********************
    i got it again

    Unnamed namespaces
    Each translation unit contains an unnamed namespace that you can add to by saying namespace without an identifier:

    namespace
    {
    class Arm { /* ... */ };
    class Leg { /* ... */ };
    class Head { /* ... */ };
    class Robot
    {
    Arm arm[4];
    Leg leg[16];
    Head head[3];
    // ...
    } xanthan;
    int i, j, k;
    }
    int main()
    {

    }

    The names in this space are automatically available in that
    translation unit without qualification. It is guaranteed that an
    unnamed space is unique for each translation unit. If you put local
    names in an unnamed namespace, you don’t need to give them
    internal linkage by making them static




    Thanks to all for help.
    I learnt a new thing
    Last edited by howhy; 08-30-2005 at 05:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM