![]() |
extern and enum size
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, |
Re: extern and enum size
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. |
Re: extern and enum size
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 };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). |
Re: extern and enum size
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); |
| All times are GMT +5.5. The time now is 12:17. |