Thread: Global and static global

  1. #1
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16

    Global and static global

    Hi,

    What is the difference between static and global static variable?
    I am not able to find any differences. Well they both seem the same to me.
    Do they differ in storage or the place where they are stored?

    Code:
    #include <stdio.h>
    
    unsigned int a ;
    static unsigned int b ;
    
    int main(void )
    {
    printf("\nGlobal : %u and %u\n",a,&a) ;
    printf("\nStatic Global : %u and %u\n",b,&b) ;
    return 0 ;
    }
    Please explain me with an example.
    Last edited by Raj 89; 11-23-2012 at 01:49 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no difference. A variable declared at file scope (i.e. outside a function) has the static storage attribute. ("global" is a misnomer, but I won't delve into that).

    Incidentally, don't use %u to print pointers, since the result is undefined behaviour. Use %p.
    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
    Jun 2009
    Posts
    120
    Quote Originally Posted by grumpy View Post
    There is no difference.
    I don't agree, mate.

    Suppose you want use those variables in other source file:
    Code:
    #include <stdio.h>
    
    extern unsigned int a; // valid
    extern unsigned int b; // invalid because of static keyword
    
    void my_variables(void)
    {
        printf("\nGlobal : %u and %p\n", a, &a); // valid
        printf("\nStatic Global : %u and %p\n", b, &b); // invalid
    }
    Note:
    Use %p for pointer types.
    Last edited by DRK; 11-23-2012 at 02:08 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if you want the difference to matter, I would expect that you need more than one source file. For example:
    Code:
    /* main.c */
    
    #include <stdio.h>
    #include "test_header.h"
     
    int main(void)
    {
        printf("Global: %u at %p\n", a, (void*)&a);
        printf("Static Global: %u at %p\n", b, (void*)&b);
    
        modify_a(123);
        modify_b(456);
    
        printf("Global: %u at %p\n", a, (void*)&a);
        printf("Static Global: %u at %p\n", b, (void*)&b);
    
        return 0;
    }
    Code:
    #ifndef TEST_HEADER_H_
    #define TEST_HEADER_H_
    
    /* test_header.h */
    
    extern unsigned int a;
    static unsigned int b;
    
    void modify_a(unsigned int new_a);
    void modify_b(unsigned int new_b);
    
    #endif
    Code:
    /* test_source.c */
    
    #include "test_header.h"
    
    unsigned int a;
    
    void modify_a(unsigned int new_a)
    {
        a = new_a;
    }
    
    void modify_b(unsigned int new_b)
    {
        b = new_b;
    }
    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 Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Quote Originally Posted by grumpy View Post
    There is no difference. A variable declared at file scope (i.e. outside a function) has the static storage attribute. ("global" is a misnomer, but I won't delve into that).
    K grumpy. I will go with using %p for pointers. But. are there not any differences? In an interview, i had been questioned that " what is the difference between global and static global" .
    What is the main difference?

    Can u please tell / describe me with any other program.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DRK View Post
    I don't agree, mate.
    That's because you don't understand the meaning of extern and static. The extern and static keywords have conflicting effects (at least on variables, at file scope).

    In C, static is a storage attribute. It also, in effect, makes the variable local to the current compilation unit. That is what happens, by default, for a variable at file scope. extern informs the compiler that the variable is defined elsewhere (e.g. in another source file). Since, by implication, an extern variable must be visible to other compilation units, extern and static are mutually exclusive (hence a compiler will complain bitterly about if you apply static and extern to the same variable).
    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.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Raj 89 View Post
    In an interview, i had been questioned that " what is the difference between global and static global" .
    What is the main difference?
    One answer to that kind of question is "there is no difference". Some interviewers do ask questions with no answer, just to see how candidates tackle it. That does give useful information about the candidates.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Raj 89 View Post
    What is the difference between static and global static variable?

    Please explain me with an example.
    Here is an example with two compilation units, driver.c and helper.c:

    driver.c:
    Code:
    #include <stdio.h>
    
    int globalstatus = 123;
    static int status = 1000;
    
    static void pstatus(void) {
        static line = 1;
        printf("driver.c: %d: status is %d, globalstatus is %d.\n", 
            line++, status, globalstatus);
    }
    
    void helper_ustatus(int);
    void helper_pstatus(void);
    
    int main()
    {
        // Print initial status
        pstatus();
        helper_pstatus();
        
        // Update (static) status and print it again
        helper_ustatus(2048);
        pstatus();
        helper_pstatus();
        
        // Update globalstatus and print it again
        globalstatus=456;
        pstatus();
        helper_pstatus();
        
        return 0;
    }
    helper.c:
    Code:
    #include <stdio.h>
    
    extern int globalstatus;
    static int status = 2000;
    
    static void pstatus(void) {
        static line = 1;
        printf("helper.c: %d: status is %d, globalstatus is %d.\n", 
            line++, status, globalstatus);
    }
    
    void helper_ustatus(int x) 
    {
        status=x;
    }
    
    void helper_pstatus(void)
    {
        pstatus();
    }
    The variable globalstatus is "global". It is defined in driver.c, therefore helper.c must declare it explicitly as "extern int globalstatus" to make use of it.

    The variable status is "global static" if we can informally call it that. As was already mentioned, the better way to say it is that it has static storage class and file scope. Notice that driver.c and helper.c each have their own status.

    The variable line in pstatus() is "function static" if we can informally call it that. It is probably better to say that it has static storage class and function scope.

    The function pstatus is static which allows each compilation unit to have its own private version of this function with the same name. All other functions are not static and thus must have unique names.

    Example compiler command: gcc -o program driver.c helper.c

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    One answer to that kind of question is "there is no difference". Some interviewers do ask questions with no answer, just to see how candidates tackle it. That does give useful information about the candidates.
    In this case there is a correct answer for the provided context of a single source file, i.e., it is not really a trick question with "no answer" since "there is no difference" is a fine answer. Of course, expanding the context to talk about multiple source files and mentioning extern would be a bonus.
    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. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  3. static global
    By rajkumarmadhani in forum C Programming
    Replies: 19
    Last Post: 01-15-2008, 01:07 PM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM