Thread: Extern Question, really confused

  1. #1
    .........
    Join Date
    Nov 2002
    Posts
    303

    Extern Question, really confused

    Hey I have question about extern I'm really confused. I think I have the general idea of how it works but I don't fully understand it. In all the examples I have looked at they have extern infront of the function prototype in the header file, but if I omit it in my header file, the program still works. The same thing happens if I have 2 .c files, if I declare a global variable in my main file, then have another file that uses that same variable shouldn't I have to use the extern keyword infront of my global variable in the other file? Cause when I omit it my compiler also does not complain. Im using gcc to compile.

    Code:
    /*********** main.c ************/
    #include <stdio.h>
    #include "addition.h"
    
    int
    main(void)
    {
    	int number1, number2, result;
    	
    	printf("Enter number: ");
    	scanf("%d", &number1);
    	printf("\nEnter number: ");
    	scanf("%d", &number2);
    	result = add(number1, number2);
    	printf("%d", result);
    	
    	return (0);
    }
     /******** addition.h ***********/
    /*** If i get rid of extern it still works, why? */
    /*extern*/ int add(int num1, int num2);
    
    /********** addition.c ***********/
    int
    add(int num1, int num2)
    {
    	return (num1 + num2);
    }
    Also if I have like say two .c files, the program still works if I remove the extern keyword infront of the variable x. Copied this from another post.
    Code:
    /* display.c */
    #include <stdio.h>
    /** If I remove extern and just say int x; it still
    /* works, why? */
    extern int x;
    
    void display( void )
    {
      printf( "%d\n", x );
    }
    
    /* main.c */
    
    int x;
    
    int main( void )
    {
      x = 5;
    
      display();
    
      return 0;
    }
    Any help would be great, confused as to why when I remove the extern keyword the program still works and I get no errors

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    um, extern is for between source file use

    ie.

    main.cpp

    Code:
    #include "incl.h"
    #include <stdio.h>
    
    int var = 10;
    
    int main(void)
    {
        printf("before: %d\n", var);
        externfunc();
        printf("after: %d\n", var);
        return 0;
    }
    incl.h

    Code:
    void externfunc(void);
    incl.cpp

    Code:
    #include <stdio.h>
    
    extern int var;
    
    void externfunc(void)
    {
        var = 25;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Just remember when calling 'extern' the variable you're trying to get needs to be global.

  4. #4
    .........
    Join Date
    Nov 2002
    Posts
    303
    If i remove the extern from your code it still compiles and it works,so extern is just for readability?

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    it wont work, at least here, I tried. what the result you get? 10 and 10 or 10 and 25?

  6. #6
    .........
    Join Date
    Nov 2002
    Posts
    303
    10 and 25.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Look here, lets give another example, 3 files:
    fnc.h
    fnc.c
    main.c

    in main.c:
    Code:
    #include <stdio.h>
    #include "fnc.h"
    
    int var = 25;
    
    int main(void) {
    	print();
    	return 0;
    }
    in fnc.h
    Code:
    void print(void);
    in fnc.c
    Code:
    #include <stdio.h>
    
    void print(void) {
    	printf("%d\n",var);
    }
    Now try compaling, it wont work, why? because it will give you an error in th file fnc.c that the compiler don't know what the hell is var. now change fnc.c to this:
    Code:
    #include <stdio.h>
    extern int var;
    
    void print(void) {
    	printf("%d\n",var);
    }
    Voilla, it works.

  8. #8
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hey Vber thanks for the reply
    In this part you have.
    Code:
    in fnc.c
    
    code:
    #include <stdio.h>
    
    void print(void) {
    	printf("%d\n",var);
    }
    Ok I know why that does not work, and you showed in your example why, and that this below does work.
    Code:
    in fnc.c
    
    code:
    #include <stdio.h>
    extern int var;
    
    void print(void) {
    	printf("%d\n",var);
    }
    Ok my question is this, if you rewrite that without using extern, it still works, like this.
    Code:
    n fnc.c
    
    code:
    #include <stdio.h>
    int var;
    
    void print(void) {
    	printf("%d\n",var);
    }
    Ok the above works without using extern, so extern is just for readability?

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    No, i guess GCC is just using a "universal" global space, i don't think the behavior you describe is standard, i could be wrong though.

    Information on this is kinda sketchy in that one thing says this one thing says that... maybe Salem can lend a hand here if he reads this.

    Well i just checked my copy of The C Programming Language, and if i'm reading this right it says that you should have to have the extern.

    For standard compliance and portability you should use it.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hey thanks for the reply No-one, I'll use it then. Your right information is kinda sketchy, I kept reading different things. I did some more research and found a definite answer to my question on the use of extern with functions here.
    Also this had a ton of more info on global variables/functions if your interested also.

  11. #11
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, I learned something new, but anyway just to see you declaring int var it won't give any idea from where this variable came, I'll still use extern

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about extern declaration
    By jam_fiveface in forum C Programming
    Replies: 5
    Last Post: 09-02-2008, 04:45 AM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  5. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM