Like Tree3Likes
  • 1 Post By grumpy
  • 1 Post By c99tutorial
  • 1 Post By laserlight

Global and static global

This is a discussion on Global and static global within the C Programming forums, part of the General Programming Boards category; Hi, What is the difference between static and global static variable? I am not able to find any differences. Well ...

  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 12:49 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    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%.

  3. #3
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    107
    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 01:08 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,208
    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).
    laserlight likes this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    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%.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    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
    21,451
    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.
    Raj 89 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12: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, 12: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

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