Question about "extern"-clarification

This is a discussion on Question about "extern"-clarification within the C++ Programming forums, part of the General Programming Boards category; Another related question: Say I have the following 3 files: -main.c -writeFunction.c -header.h And I want main.c and writeFunction.c to ...

  1. #16
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Another related question:
    Say I have the following 3 files:
    -main.c
    -writeFunction.c
    -header.h

    And I want main.c and writeFunction.c to have a common global variable.
    So that I can modify the variable in main, and then the variable in the function in "writeFunction.c" would be updated as well.

    One possibilty is to define the variable in for example main, and in "header.h", include
    Code:
    extern int globalCommonVariable;
    But, would it be correct to instead drop defining the globalCommonVariable, and instead just do like this in the header:
    Code:
    int globalCommonVariable;
    I have compiled and tested it, and if I do it like this, it works, and the variable is shared, with only 1 definition, and no use what so ever of extern.
    However...is it safe, and is it correct according to standards and all that stuff?

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    No, it's not correct because you'd be defining the variable twice in the two source files, and you cannot have variables with the same name in multiple source files on a global level.
    Therefore you have to define it in our source file and use a declaration everywhere you use so the compiler knows it exists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I'll give you my 3 files...I've compiled and linked, and it works :S

    main.c
    Code:
    #include "headerFil.h" // get the global variable "test"
    
    int main(int argc, char *argv[]) {
      test = 10; // Modify the global variable 
      moo(); // Write the global variable, using a function from fil1.c
      system("PAUSE");	
      return 0;
    }
    fil1.c
    Code:
    #include "headerFil.h" // Get the global variable "test"
    
    void moo(void) {
      printf("variabelen er:%d\n", test); // Just print the global variable
    }
    headerFil.h
    Code:
    #ifndef MOFF
    #define MOFF
    void moo(void);
    int test; // Global variable to be used in 2 files
    #endif

    So..is it just because my compiler/linker are very friendly, and smart, that this works?
    Last edited by Drogin; 08-30-2009 at 10:30 AM.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Drogin View Post
    Another related question:
    Say I have the following 3 files:
    -main.c
    -writeFunction.c
    -header.h

    And I want main.c and writeFunction.c to have a common global variable.
    So that I can modify the variable in main, and then the variable in the function in "writeFunction.c" would be updated as well.

    One possibilty is to define the variable in for example main, and in "header.h", include
    Code:
    extern int globalCommonVariable;
    But, would it be correct to instead drop defining the globalCommonVariable, and instead just do like this in the header:
    Code:
    int globalCommonVariable;
    I have compiled and tested it, and if I do it like this, it works, and the variable is shared, with only 1 definition, and no use what so ever of extern.
    However...is it safe, and is it correct according to standards and all that stuff?
    Some compilers will save you from yourself. For instance, I just tried with gcc; if I only typed "int globalCommonVariable" I got away with it -- if I did "int globalCommonVariable = 3" I got a duplicate symbol error.

  5. #20
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by tabstop View Post
    Some compilers will save you from yourself. For instance, I just tried with gcc; if I only typed "int globalCommonVariable" I got away with it -- if I did "int globalCommonVariable = 3" I got a duplicate symbol error.
    That's not saving you from yourself, thats standard behavior. declaration can take place in multiple places, initialization can occur only once.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by abachler View Post
    That's not saving you from yourself, thats standard behavior. declaration can take place in multiple places, initialization can occur only once.
    And since global variables are automatically initialized to zero .... what's your point?

  7. #22
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    So, that the code from this headerfile works, is because the compiler is saving me?
    Code:
    #ifndef MOFF
    #define MOFF
    void moo(void);
    int test; // Global variable to be used in 2 files
    #endif

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Drogin View Post
    So, that the code from this headerfile works, is because the compiler is saving me?
    Code:
    #ifndef MOFF
    #define MOFF
    void moo(void);
    int test; // Global variable to be used in 2 files
    #endif
    Well, you know, maybe I'm wrong here. I've been reading through trying to decide whether int test here is a definition or just a declaration. For some reason I had thought that all file scope declarations were definitions, but I can't find that now, or anything close to that. This might be a difference between C++ and C, since in C++ this is illegal. More updates as events warrant.

    UPDATE: Oh hey, this is in the C++ forum! So, yes, this is illegal in C++.
    Last edited by tabstop; 08-30-2009 at 11:20 AM.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by tabstop View Post
    UPDATE: Oh hey, this is in the C++ forum! So, yes, this is illegal in C++.
    Ah, but the OP's example is C, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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