Thread: what's difference between global and static variable declare outside function

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    20

    what's difference between global and static variable declare outside function

    Hi,
    I am little bit confused I don't understand difference between global variable and static variable that declare outside function.

    Code:
    #include<stdio.h>
    
    int X = 10;  //Global variable 
    static int Y = 100; // static variable 
    
    
    int main ()
    {
    	while (1)
    	{
    		return 0;
    	}
    	
    }
    Both X and Y variable are only accessible within source file
    Both X and Y variable only alive until program exit

    I don't understand difference between global variable and static variable that declare outside function.

    Is there any difference between them in standard c ?

    Are they same or different?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    It might be helpful to try a sample program consisting of one source file:
    Code:
    #include <stdio.h>
    
    void foo(void);
    
    int x;
    static int y;
    
    int main(void)
    {
        x = 10;
        y = 100;
        printf("x=%d; y=%d\n", x, y);
        foo();
        printf("x=%d; y=%d\n", x, y);
        return 0;
    }
    and another source file:
    Code:
    extern int x;
    static int y;
    
    void foo(void)
    {
        x = 20;
        y = 200;
    }
    If you compile and link them to build an executable, then run it, you'll get output like this:
    Code:
    x=10; y=100
    x=20; y=100
    So, the global variable named x has what we call external linkage, hence my use of the extern keyword to explicitly specify this in its declaration in the second source file: the name x (unless shadowed by a more local declaration of the name x) refers to the same object across translations units (i.e., across different source files + their respective included headers). The file scope variable named y that is declared static has what we call internal linkage: the name y refers to the same object in the same translation unit. That's why we see that the value of x has changed but the value of y remained the same: the x that foo() modified is the same x that the main function referred to, whereas the y that foo() modified is a different variable that also happens to be a file scope variable named y.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,618
    > Both X and Y variable are only accessible within source file
    No.

    Another source file can do
    extern int X;
    and gain access to your variable declared in this source file.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2020
    Posts
    20
    Quote Originally Posted by Salem View Post
    > Both X and Y variable are only accessible within source file
    No.
    Thank you both of you
    yes we can use extern storage class but what if I write the program as shown in opening post

    Is there any difference between them in standard c ?

    Are they same or different?

  5. #5
    Registered User
    Join Date
    Jul 2020
    Posts
    20
    Quote Originally Posted by laserlight View Post
    It might be helpful to try a sample program consisting of one source file:
    No doubt you have given such wonderful answer but my question was does both X and Y are same or difference because I don't see any difference in accessibility and life time

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    X has external linkage by virtue of being declared at file scope without being declared static, i.e., X is different from Y in exactly the way that I demonstrated.

    As for your program from post #1: since you only have one translation unit, there's no real difference in the context of that program. But then since you only have one function, there's also no real difference between a file scope variable and a local variable in the context of that program. Then again you don't even use those variables, so there's no real difference between declaring the variables and not declaring any. So you need to understand things in their proper context, not only in the forced context that you have in mind.

    EDIT:
    Quote Originally Posted by Djsarkar
    No doubt you have given such wonderful answer but my question was does both X and Y are same or difference because I don't see any difference in accessibility and life time
    You don't see any difference in accessibility? Look at the output. It is obviously different! As for lifetime: both variables have static storage duration (i.e., this is not dependent on the use of the static keyword for variables declared at file scope), so their lifetime is that of the program.
    Last edited by laserlight; 11-15-2020 at 12:28 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2020, 04:27 AM
  2. How to declare a global variable in a function?
    By exclusive in forum C Programming
    Replies: 5
    Last Post: 11-06-2011, 04:28 AM
  3. Global/Static function/variable slower to access?
    By JohnLeeroy in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2011, 11:56 PM
  4. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  5. Replies: 8
    Last Post: 09-27-2010, 04:11 PM

Tags for this Thread