Thread: scope of a variable

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    scope of a variable

    Hi,

    if i write a program like this
    Code:
    void test(int i);
    
    int main(void)
    {
    test(3);
    }
    
    int j;
    void test(int i)
    {
     printf("%d",i);
    }
    My doubt is about the "j" variable. Does it come under file scope? Does file scope mean it should be visible to all the functions in the file? Should we ever use this kind of definition for "j"?

    Thanks and regards,
    Satya

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, j has file scope. That means it is accessible to all functions in the same source file that are defined AFTER it is declared. In your example, it is therefor accessible by test() but not by main().

    There are circumstances where such a definition is useful. However, there are more circumstances where such a definition is bad practice, and alternatives are better.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by grumpy View Post
    Yes, j has file scope.
    The "j" variable has file scope but still main cannot access it. Is it not contradictory? So if someone says a variable has file scope, then i should also see where it is declared that is after which function.

    Thanks and regards,
    Satya

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Satya View Post
    The "j" variable has file scope but still main cannot access it. Is it not contradictory?
    Nup. The END of the variable's scope corresponds to the end of the file, hence the name.

    There is another rule that says (essentially - I'm oversimplifying) no code statement can access a variable before that variable is seen by the compiler.
    Last edited by grumpy; 05-21-2014 at 07:57 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  2. variable scope
    By happyclown in forum C Programming
    Replies: 4
    Last Post: 03-02-2009, 06:48 AM
  3. Variable Scope
    By Matty_Alan in forum C Programming
    Replies: 3
    Last Post: 11-23-2008, 03:28 PM
  4. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  5. Variable Scope
    By tinkerbell20 in forum C++ Programming
    Replies: 5
    Last Post: 06-22-2005, 10:31 PM