What is the difference between extern and static
The next table summarizes the principal features of each storage class which are commonly used in C programming. Skip to content. What is Storage Class in C? A storage class in C is used to describe the following things: The variable scope.
The location where the variable will be stored. The initialized value of a variable. A lifetime of a variable. Who can access a variable? Thus a storage class is used to represent the information about a variable. NOTE: A variable is not only associated with a data type, its value but also a storage class. Storage class Purpose auto It is a default storage class. Auto Storage Class in C The variables defined using auto storage class are called as local variables. Extern Storage Class in C Extern stands for external storage class.
They can also be used as a global variable Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined. Static global variables are global variables visible only to the file in which it is declared. Register Storage Class in C You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of RAM to have quick access to these variables.
A variable can be known or seen by all functions within a program. A local variable defined in a function can also be declared as static. This causes the same behaviour as if it was defined as a global variable, but is only visible inside the function. This means you get a local variable whose storage is permanent and thus retain its value between calls to that function.
I'm no C expert so I might be wrong about this, but that's how I've understood static and extern. Hopefully someone more knowledgable will be able to provide you with a better answer.
You can apply static to both variables and functions. There are two answers that discuss the behaviour of static and extern with respect to variables, but neither really covers functions.
This is an attempt to rectify that deficiency. By default, functions in C are visible outside the translation unit TU — basically the C source file and included headers in which they are defined. Such functions can be called by name from any code that notifies the compiler that the function exists — usually by a declaration in a header.
If a source file includes the header, it can call the functions. When the program is linked, the correct library must be specified to satisfy the function definition. Fortunately, the C compiler automatically provides the library that provides most of the functions in the standard C library and it usually provides a lot more functions than just those. Note that external functions should be declared in headers. Each external function should be declared in one header, but one header may declare many functions.
The header should be used both in the TU where each function is defined and in each TU that uses the function. You should never need to write a declaration for a global function in a source file as opposed to a header file — there should be a header to declare the function and you should use that header to declare it. As an alternative to generally visible functions, you can make your own functions static. This means that the function cannot be called by name from outside the TU in which it is defined.
It is a hidden function. The primary advantage of static functions is hiding details which the outside world doesn't need to know about. It is a basic but powerful information hiding technique. You also know that if a function is static, you do not need to look for uses of the function outside the current TU, which can greatly simplify the search.
However, if the functions are static , there can be multiple TUs which each contain a definition of a function with the same name — each TU has its own function, which may or may not do the same thing as a function with the same name in a different TU. In my code, I qualify all functions except main with the keyword static by default — unless there's a header that declares the function. If I subsequently need to use the function from elsewhere, it can be added to the appropriate header and the keyword static removed from its definition.
It is possible, but very inadvisable, to declare a function inside the scope of another function. They're also a maintenance liability. The declaration in processor suffices for it to use subprocess , but is otherwise unsatisfactory. The extern declaration before the definition is necessary if you use GCC compiler options such as:.
It's another reason I make most functions static, and define the functions before they're used. The alternative is to declare static functions at the top of the file and then define them in whatever order seems appropriate. There are some merits to both techniques; I prefer to avoid the need to declare and define the same function in the file by defining before use. Note that you cannot declare a static function within another function, and if you attempt to define a function such as subprocess as a static function, the compiler gives an error:.
Since functions that are externally visible should be declared in a header, there is no need to declare them inside a function, so you should never run into this as a problem. Again, the extern is not necessary in the function declaration inside the function; if omitted, it is assumed.
This can lead to unexpected behaviour in novice programs here on SO — you sometimes find a function declaration where a call was intended. With GCC, the option -Wnested-externs identifies nested extern declarations. The extern in the declaration of the function is optional but explicit. I normally use it in a header file to match the declaration of those rare global variables — where the extern is not optional but mandatory.
Many people disagree with me on this; do as you wish or must. Now what about static functions? Suppose the TU reveal. Then, in another TU openness. Only the TU that defines the hidden function can use it directly. Making the main definition and putting extern property when i want to use the variable in other class.
Thanks you all. You must have exactly one, no more and no less, definition that does not have 'extern'. So, use the third one. It is analogous with functions: prototypes are extern statements for functions, and the definition can only appear once out of everywhere. Last edited on Apr 5, at pm UTC. Apr 9, at am UTC. The extern in the declaration of the function is optional but explicit. I normally use it in a header file to match the declaration of those rare global variables — where the extern is not optional but mandatory.
Many people disagree with me on this; do as you wish or must. Now what about static functions? Suppose the TU reveal. Then, in another TU openness. Only the TU that defines the hidden function can use it directly. However, if there's a function in reveal. Obviously, that's a function that takes no arguments and returns a pointer to a function that takes an int argument and returns no value. No; it isn't pretty. One of the times it makes sense to use typedef on pointers is with pointers to functions reveal2.
See Is it a good idea to typedef pointers for a general discussion on the subject of typedef and pointers; the short summary is "it isn't a good idea except perhaps with function pointers".
Yes, it is legitimate but very unusual to define the function with an explicit extern — I very, very seldom do it, but here it emphasizes the role of extern and contrasts it with static. You can remove the extern without changing the meaning of the program. However, the revealer function declared in reveal. Python Javascript Linux Cheat sheet Contact.
What is the difference between static and extern in C? TL;DR Use static functions whenever possible. Only declare external functions in headers.
0コメント