
www.Usenet.com
| <-- __Chronological__ --> | <-- __Thread__ --> |
"Bradley" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, I am an Assembly language programmer of 12 years, now starting to > code in C. I am still a C newbie and have some confusion. C is just high-level assy. You should do well when you get this syntax figured out. the "static" keyword means two totally differnt things in C (and C++ added yet a third meaning :-( ). 1) static int foo(void) is an example of a function that can only be referenced (called) by code in the same C file (translation unit) that foo is in. The linker will not be able to resolve foo called by other C file code to this version of foo. This form of static applies to variables too. (e.g. static float x is invisible to other C modules) 2)Inside a function, static means that the variable retains its value between calls of the function. As you know, normally (non static) automatic varaibles are allocated on the stack and can have any old garbage in them when a function begins execution. static allocates the variable elsewhere in RAM. corollary to 1) linkage (the ability of the linker to resolve a reference to function or variable) is global by default (works across all source files). The static keyword *prevents* the linker from resolving a reference. This makes static good for hiding code and data from modules that shouldn't be messing with them. Think of it as an early form of encapsulation. The "extern" keyword tells the compiler about a function or variable that is not defined (coded or allocated ) in the currrent C file. Using extern, you tell the compile that foo is a function returning int that takes no arguments. The compiler can then call foo with the appropriate boilerplate (e.g. don't push anything onto the stack but do allow the returned int to be assigned). Good practices: 1) read K&R and keep it handy. bookmark the precedence table and the declaration syntax pages. 2) turn the compiler warning level up to full (use lint too) 3) encapsulate functional units in separate C files. Each C file should have an H file that defines the functions and variables that other code needs to use the C file AND NOTHING ELSE. If you find that you need to put an extern into a C file to refernce a different C file then take a little time to ponder why your H file doesn't provide an adequate interface. 4) step through some of your code on the target system in assy mode. If you can't do that, then at least look at the assy output of the compiler (usually a command line arg will generate an assy listing for you). The assy code never lies! good luck, Bob > > I am finding that I am getting bogged down with uses of static / > extern for functions and variables. I am also getting a bit confused > with function prototyping and header files. I have searched the > internet quite a bit but I am still confused. > > I think that I know the following; (Please clarify) > > Static variables are local to the .c file. Static variables also can > stay around after the function is exited. > A variable declared outside of Main is global and a .c file that wants > to use it must extern it. > > Static functions are visible to the .c file. My problem here is why > declare them static? Isn't static assumed? > A function can be called by other .c files if its prototype is > External. > > I have read that it is good C practice to code prototypes for each > function. Should I code the prototypes in a .h file associated with > the .c file? Where do I extern the prototype if I plan to use the > function in multiple .c files ? > > Any help / suggestions would be greatly appreciated. > > thank you > > Bradley
| <-- __Chronological__ --> | <-- __Thread__ --> |