Thread: extern variable is not working as expected

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    extern variable is not working as expected

    extern variable tell to compiler about scope

    I've declare variable x in file1.c
    file1.c
    Code:
    extern int x;
    I want use the variable x in main.c file

    main.c
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        x = 10;
        printf("x=%d ", x);
        return 0;
    }

    when i compile code I get error: 'x' undeclared (first use in this function)

    What's wrong in code ? How can I use variable in main.c that is declarered in another file

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    extern int x;
    Put the above code in a header file.

    Include the header file by the two .c files.

    Code:
    int x;
    Put the above code in only one of the .c files

    Edit: The above method uses global scope; which is what the "extern" keyword is normally used for. I know of no way to use extern in C and not have it be global; but an C expert might.

    Tim S.
    Last edited by stahta01; 08-21-2021 at 12:44 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    Code:
    extern int x;
    Put the above code in a header file.

    Include the header file by the two .c files.

    Tim S.
    sorry I didn't understand what you mean.

    Do you mean I need three files main.c file1.c and file1.h

    Code:
    #include<stdio.h>
    #include"file1.h" 
    int main(void)
    {
        x = 10;
        printf("x=%d ", x);
        return 0;
    }
    What should I keep inside header file ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    // file1.c
    int x;
    
    
    // file1.h
    extern int x;
    
    
    // main.c
    #include<stdio.h>
    #include"file1.h" 
    int main(void)
    {
        x = 10;
        printf("x=%d ", x);
        return 0;
    }
    Then compile with say
    gcc main.c file1.c
    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.

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by Salem View Post
    [code]
    Then compile with say
    gcc main.c file1.c
    Thank you so much. Can you tell me the reason why variable x being declared in two place (file1.c and file1.h)

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Rahul11 View Post
    Thank you so much. Can you tell me the reason why variable x being declared in two place (file1.c and file1.h)
    Because if you do it that way; the compiler checks for mistakes like type changes and typos; when in the future the program is modified it helps from spending hours looking for stupid mistakes.

    FYI: It is only defined in one place and declared in the other place.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    The compiler must know about the identifiers it is using... So, you can do:
    Code:
    // file1.c
    int x = 10;
    Code:
    // main.c
    
    // The compiler must know that 'x' exists elsewhere!
    extern int x;
    
    int main( void )
    { printf( "x=%d\n", x ); }

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Quote Originally Posted by Rahul11 View Post
    sorry I didn't understand what you mean.

    Do you mean I need three files main.c file1.c and file1.h
    What should I keep inside header file ?
    First of all, you should avoid global variables if at all possible. Define them in main() and pass them to functions called by main().

    A simple program may only consist of a few functions in one .c file. When you create larger programs, you will need to put functions in several files. Then you will need a header file to place the extern function declaration/prototypes, struct declarations, etc... along with minimal global variables.

    You would place the extern variables in the header file. A very simplistic example of a multiple module program, programmed on Linux:

    foo.h
    Code:
    #ifndef FOO_H
    #define FOO_H  // To prevent multiple inclusion of the header file
    
    extern int result; // Declaration only, will be defined later
    
    int square(const int val); // Function prototype (Declaration)
    
    #endif // FOO_H
    foo.c
    Code:
    #include <stdio.h>
    
    #include "foo.h"  // Your variable & function declarations
    
    int main(void)
    {
       int num = 10; // Always initialize local varaibles
    
       result = square(num);
    
       printf("num == %d, result == %d\n", num, result);
    
       return 0;
    }
    bar.c
    Code:
    #include "foo.h"
    
    // For illustration purposes only, normally would be defined in foo.c
    int result = 0;   // Definition of the global variable
    
    int square(const int val)  // Function prototype (Declaration)
    {
       return val * val;
    }
    Output:
    Code:
       $./foo
       num == 10, result == 100

  9. #9
    Registered User
    Join Date
    May 2021
    Posts
    66
    I'm getting confused with definition of extern keyword. My understand is extern is keyword that tell a compiler variable or function is define in another file

    we have three files
    file1.h
    file1.c
    main.c

    which is that another file in my example file1.c or file1.h ?

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Quote Originally Posted by Rahul11 View Post
    I'm getting confused with definition of extern keyword. My understand is extern is keyword that tell a compiler variable or function is define in another file

    we have three files
    file1.h
    file1.c
    main.c

    which is that another file in my example file1.c or file1.h ?
    Code:
    extern int x;
    extern is used to DELCARE a variable, and specify that it is of type int. This line should go into file1.h.

    The line:
    Code:
    #include "file1.h"
    should be in both "file1.c and "main.c".

    In one of the two files, file1.c or main.c, but NOT both, you should have a global variable DEFINED as:
    Code:
    int x = 0;
    A DECLARATION does not create the variable in memory. It is information for the compiler so when it sees the variable in use, it knows how to handle it.

    A DEFINITION creates the actual variable in memory. Both the declaration and definition of the variable, must match in name and data type.

    When you compile the two .c files together, then the linker will resolve the DECLARATION, DEFINITION, and the use of "x" in main() all to the same variable.

    If you are not taking a course from a qualified instructor, or trying to learn from inadequate online videos or tutorials:

    You need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    This would give you a detailed tutorial about this, and all the other features of the C Programming Language.

    Hope this helps to clear up your confusion.
    Last edited by rstanley; 08-22-2021 at 08:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-13-2020, 09:48 PM
  2. Replies: 9
    Last Post: 05-07-2017, 12:29 PM
  3. does a variable can be a static and extern both ?
    By gunjansethi in forum C Programming
    Replies: 2
    Last Post: 03-09-2011, 08:51 AM
  4. extern variable
    By edesign in forum C Programming
    Replies: 4
    Last Post: 06-10-2009, 05:51 AM
  5. extern variable assignment
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 07:41 AM

Tags for this Thread