Thread: Scope of global variable

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    Scope of global variable

    I have a doubt about the global variable. The global variable is the variable that is declare out side of function

    Code:
    #include<stdio.h>
    int x; // global variable 
    void foo ()
    {
        x = 0;
        x++;
        
        printf( "x = %d", x);
    }
    
    int main()
    {
    
    foo();
    
    return 0;
    }
    My thought is that if I made the variable x a global variable in main.c, can I use it in another file foo.c?

    main.c
    Code:
    #include<stdio.h>
    
    int x; // global variable 
    
    int main()
    
    {
      foo();
     return 0;
    }
    foo.c
    Code:
    void foo (){
        x = 0;
        x++;
        
        printf( "x = %d", x);
    }
    I think that I can access the x only in main.c

    Can we access x from foo.c file ?
    Last edited by gajya; 02-18-2020 at 03:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Yes, a global variable indeed has global scope, i.e., every use of that name that has not been shadowed by another variable whose name has a more local scope refers to that global variable.
    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
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by laserlight View Post
    Yes, a global variable indeed has global scope, i.e., every use of that name that has not been shadowed by another variable whose name has a more local scope refers to that global variable.
    But my experiment is not giving such a result

    result

    foo.c: In function 'foo':
    foo.c:3:2: error: 'x' undeclared (first use in this function)
    x = 0;
    ^
    foo.c:3:2: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:6:2: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
    printf( "x = %d", x);
    ^~~~~~
    foo.c:6:2: warning: incompatible implicit declaration of built-in function 'printf'
    foo.c:6:2: note: include '<stdio.h>' or provide a declaration of 'printf'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    At the top of foo.c, declare x:
    Code:
    extern int x;
    You also need to #include <stdio.h> in foo.c
    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
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by laserlight View Post
    At the top of foo.c, declare x:
    Code:
    extern int x;
    You also need to #include <stdio.h> in foo.c
    ohh This means that I can only use the global variable in the same file where it is declared. In this program, I have declare x in main.c , so I cannot use x in another file foo.c.


    If I want to do this, I have to use an extern keyword

    main.c
    Code:
     #include<stdio.h>
    
    int x; // global variable 
    
    
    int main()
    
    
    {
    
    
    foo();
    
    
    return 0;
    }
    foo.c
    Code:
    #include<stdio.h>
    
    extern int x;
    
    
    void foo ()
    {
    	x = 0;
    	x++;
    	
    	printf( "x = %d", x);
    }
    C:\Users\gj\AppData\Local\Temp\cclHNYux.o:foo.c.text+0x8): undefined reference to `x'
    C:\Users\gj\AppData\Local\Temp\cclHNYux.o:foo.c.text+0x11): undefined reference to `x'
    C:\Users\gj\AppData\Local\Temp\cclHNYux.o:foo.c.text+0x19): undefined reference to `x'
    C:\Users\gj\AppData\Local\Temp\cclHNYux.o:foo.c.text+0x1e): undefined reference to `x'
    collect2.exe: error: ld returned 1 exit status

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,175
    It looks like you tried to compile without your main.c

    Code:
    $ head -20 foo.c main.c
    ==> foo.c <==
    #include<stdio.h>
     
    extern int x;
     
     
    void foo ()
    {
        x = 0;
        x++;
         
        printf( "x = %d", x);
    }
    
    ==> main.c <==
    #include<stdio.h>
     
    int x; // global variable 
     
     
    int main()
     
     
    {
     
     
    foo();
     
     
    return 0;
    }
    $ gcc foo.c main.c
    main.c: In function ‘main’:
    main.c:12:1: warning: implicit declaration of function ‘foo’ [-Wimplicit-function-declaration]
     foo();
     ^
    $ ./a.out 
    x = 1$
    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.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    It looks like you tried to compile without your main.c
    it works if I declare function before main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access variable from other scope without global declaration?
    By unixmania in forum C++ Programming
    Replies: 1
    Last Post: 08-08-2013, 03:03 AM
  2. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  3. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 02:25 PM
  4. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  5. Global scope
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 08:51 PM

Tags for this Thread