external variable

This is a discussion on external variable within the C Programming forums, part of the General Programming Boards category; I want to be able to declare a global variable in a header/source file and then use it in some ...

  1. #1
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555

    external variable

    I want to be able to declare a global variable in a header/source file and then use it in some other source file which has #included it, like

    Code:
    sth1.c
    -----------
    int integer;
    
    
    sth2.c
    ----------
    #include <stdio.h>
    #include <sth1.c>
    
    char* main ()
    {
    	ineger = 45;
    	printf("%d", integer);
    
    	return "o-|-<";
    }
    I looked around in some files like errno.h and could find the "extern" keyword plus other crazy stuff like _RTLENTRY, _EXPFUNC and _UNDERSCOREZZ.
    I looked around some and it seemed to me that extern does what I want, but it didn't work. I would get
    Code:
    Error: Unresolved external '_integer' referenced from sth.obj
    Compiler: bcc32 5.6.1

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Code:
    sth1.c
    -----------
    int integer;
    
    
    sth2.c
    ----------
    #include <stdio.h>
    #include <sth1.c>
    /*AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH*/
    char* main ()
    {
    	ineger = 45;
    	printf("%d", integer);
    
    	return "o-|-<";
    }
    It is int main(). You will have to extern integer
    Woop?

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In your header file (an .h file), which is included in C files, put the declaration:
    Code:
    extern int test;
    In one C file put the variable definition:
    Code:
    int test;
    Do not include one .c file in another.

    http://www.eskimo.com/~scs/C-faq/q1.7.html

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Oh, so that's it. I had put 'extern' in a .c-file.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    No you put an extern in a .h file
    something like this
    Code:
    .h file
    --------------------------------------
    extern integer;
    
    .c file1
    --------------------------------------
    #include <stdio.h>
    #include <sth1.c>
    int main ()
    {
    	ineger = 45;
    	printf("%d", integer);
    
    	return 0;
    }
    
    .c file2
    --------------------------------------
    int integer;
    Woop?

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    integer (integer is not an appropriate variable name anyway).

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And you need a type here:
    Code:
    extern integer;
    ->
    Code:
    extern int integer;
    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.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anonytmouse
    integer (integer is not an appropriate variable name anyway).
    Why? It isn't a reserved keyword.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by quzah
    Quote Originally Posted by anonytmouse
    integer (integer is not an appropriate variable name anyway).
    Why? It isn't a reserved keyword.
    Quzah.
    I would say that using a type name as a variable name is confusing and not good style. It is also a common typedef. However, it is a style issue, so to each his own.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by anonytmouse
    It is also a common typedef.
    Now that is evil. Yet another reason for me to abhor typedefs.
    Last edited by Dave_Sinkula; 12-23-2005 at 08:53 PM.
    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.*

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    True. You didn't say it was illegal, so I'l give you that. I agree, it isn't a very good lable for a variable. Generally speaking, they should be meaningful. I really just wanted to get you to explain to them why it wasn't a good idea. Mainly because I'm usually pedantic, and bored.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Dave_Sinkula
    Now that is evil. Yet another reason for me to abhor typedefs.
    What's wrong with typedefs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VC++ a POS
    By Welder in forum Windows Programming
    Replies: 40
    Last Post: 11-07-2007, 02:07 PM
  2. Installing Direct X
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 11-15-2006, 06:18 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Help with error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-17-2002, 09:36 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM

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