extern and enum size

Muhammad.Ammar's Avatar, Join Date: Mar 2009
Newbie Member
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,
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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).
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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).