extern and enum size

Discussion in 'C' started by Muhammad.Ammar, Mar 6, 2009.

  1. Muhammad.Ammar

    Muhammad.Ammar New Member

    Joined:
    Mar 6, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello Everyone

    Hope you will be fine and good.

    First Question
    Can anyone please provide me detailed tutorial on extern in ANSI C.

    What they are?
    Their purpose/benefits?
    How to use extern with functions, structures, variables?
    Everything other thing about externs.
    Kindly give examples as many as you can.

    Second Question

    what is the default size of an enum.
    can we change the default size of enums and how.
    for example: if i want to use enums of 1 byte, how to do this?

    Regards,
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I would try to add as much I know about externs

    Its something like declaration of global variable which can be referenced across files.

    Say you have declared a variable x in file1.c and would like to use the same variable ( Not just the variable name ) into some other file then in that other file you need to use the word extern to use the first variable declared in file1.

    Same is the case with the functions / structures

    Default size of enum is the maximum contained element size.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    There's not much more to say about extern than what shabbir's already posted, in fact I think it's fair to say he's nailed it. extern is an indicator to the compiler that the variable definition that follows exists in another file so it shouldn't allocate space for it. That's all.

    As for enums, see http://www.cprogramming.com/tutorial/enum.html
    It doesn't make sense to want to use enums of 1 byte; this suggests you don't know what they're for. enum is an integer-like type that enumerate a list of related constants and you first define the enum with a name, e.g. enum X { A,B,C};, then you define a variable of type enum X and you can set it to A,B or C. The compiler handles the type, so it would be inadvisable to assume anything about the size taken up by such a variable; instead use a switch for example:
    Code:
    enum X { A,B,C };
    enum X foo;
    int bar;
    switch (x)
    {
    case A: bar=1; break;
    case B: bar=2; break;
    case C: bar=3; break;
    }
    
    Because if you just do something like int quux=(int)foo, which should compile OK, and someone comes along later and changes the enum to enum X { A=1,B=10,C };, which is valid, but now quux has completely different values, whereas with the switch above, regardless of the actual values of A,B and C, bar predictably contains 1,2 or 3.

    Also I would suggest that int quux=(int)foo is an *optimisation* and should not be done until code profiling has shown that the above switch statement is a bottleneck (which it will almost certainly never be).
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The only other use for extern that I can think of is in C++ where you want to call a function that was written in C and whose name wasn't mangled, e.g.
    Code:
    extern "C" someCfunc(int a,int b,int c);
    
    When C++ generates the code for someCfunc() it won't mangle the name and so you won't get a "missing symbol" error for something cryptic like someCfunc#III (actual name will depend on the compiler).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice