Results 1 to 8 of 8

Thread: extern

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post extern

    must we always do

    extern int a;

    when declaring and not defining in C++?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Do not use extern all the time. You need it only in some case that you do not have to define it but only to declare the variable.

  3. #3

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Would you like to post a situation like that please?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Code:
    // in source file A
    
    unsigned long x;
    Code:
    // in source file B
    
    extern unsigned long x;
    
    ...
    
    x = 10;
    The extern keyword in the variable declaration means that we are NOT declaring the variable here, but that it is declared somewhere else. The compiler says, Ok, and happily compiles, and leaves it to the linker to find the variable it should be pointing to.

    Z.

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    But.

    It has to be a global variable.

    ie., it is not defined inside any function, including main();

  6. #6

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    does that mean whenever I defining a extern, unsigned variable, I always have to define it outside any function?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by prog_tom
    does that mean whenever I defining a extern, unsigned variable, I always have to define it outside any function?
    I don't think so. But the actual variable you want to use should be global.
    So this one should be global if you want to use extern:
    PHP Code:
    int myvar
    Where this one can be anywhere as long you follow the above rule(I think):
    PHP Code:
    extern int myvar
    Baaaaaaaaah

  8. #8
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    No. It should be global as well in the sub-module.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width