Thread: global variables in C

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    global variables in C

    do you always use "extern" to mark something as a global variable??

    and does the name has to be DebugMode?

    so extern int DebugMode?? or can I name it extern int just_an_int??

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Global variables are just variables with global scope (ie, all functions can access them).
    So yes, they can have whatever name they please.
    The "extern" keyword is used to access a global variable defined in another .c file, otherwise the compiler will complain it can't find the variable.
    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. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay so I have to declare :

    int just_an_int = 10;

    in my .c file and whenever I want to use it on my .h file for an example.. I just use extern just_an_int??

    and will the just_an_int in the .h file will have a value of 10 too?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Never put code (this is for C) in header files.
    But if you want to access your global just_an_int in file X.c from Y.c, then yes, you must first put an "extern int just_an_int;" before using it.
    It is the very same variable from X.c that you're using.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and I can use it in a .h file too right??

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can put the "extern" in the header, but never place any code in a header.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay..will do

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's how global variables work.

    When you declare a global variable in one source file, everything is just fine; you can use the variable where ever you like in that file (beyond its point of definition). However, if you want to use that same variable in another file, you have to put "extern" in front of it, so that the compiler doesn't try to allocate memory for the variable in this other file. Keep in mind that you want only one variable in existence, so the compiler, which cannot know about other source files, has to be told when to actually allocate space for the variable.

    (If you don't use extern, the linker will complain when it discovers that you've tried to allocate memory for the same variable name in two different places.)

    So . . . that's all extern does. It says, this variable is declared externally, not here.

    As such, you can't assign a value to an extern variable when it is declared. (Doing so removes the influence of "extern", and usually generates a warning.) You can only do this for non-extern global variables, i.e.
    Code:
    int x = 10;
    Now, when you go
    Code:
    extern int x;
    in another source file, you'll be accessing the same variable. So it will also have the value 10. And any changes to x, in any source file that has its declaration, will be reflected in all the other source files.

    Some notes: if you want two copies of a global variable in two different source files, you can use the "static" modifier in place of "extern". And, of course, global variables aren't the best things to use most of the time.

    [edit] A few replies appeared whilst I was typing this up . . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so can you explain about static?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    static is basically, "make this variable visible throughout this source file, but not in any other source files."
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    got it! so why must we put a static in front of the type ??

    why don't we just use the name of the file, say:

    Code:
    int integ;
    
    int main()
    {
    .........
    }
    so therefore integ can only be accessed inside the file right?

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Never put code (this is for C) in header files.
    Header files are exactly where extern declarations should go.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Absolutely. Prototypes, declarations, etc, goes into headers. But not implementations, definitions or other general code.

    Anyway, you don't need static. If you want a global variable only to be available in the source file it was defined in, then you may use static. That will prevent other source files from accessing it (even with extern).
    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.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I see so static are only used inside the file and global can be used in other file using the extern files? is that the difference?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM