Essay sample library > C: What is the use of 'extern' in header files?

C: What is the use of 'extern' in header files?

2023-06-26 09:38:07

If this sounds like a question asked many times, please forgive but I guarantee you this is a bit different.

I use code blocks for C programming, and recently I began to wonder if someone is using header files in C. I understand that it is used for declaring and defining variable structures. But this is what I tried, now I am confused

Now, my point is that even if I declared extern int type 1 is commenting on, extern invalid show (); scratch file test1.h I get warnings and errors at compile and run We could not acquire it. If you can directly access int one and void show () from headertest.c, they have implicit external links, so what is the use of the Header file here? Why does anyone declare variables or functions as extern in the header file, or you can directly access them instead.

The C ++ code file (extension .cpp) is not the only file common to C ++ programs. Another type of file is called a header file, sometimes called an include file. Although the extension of the header file is usually .h, you can see that the extension is .hpp or has no extension at all. The purpose of the header file is to save declarations of other files being used. This program displays "Hello, world!" With cout on the console. However, since this program does not define cout, how does the compiler recognize what cout is? The answer is that cout is declared in the header file "iostream". When using #include lines, you need to copy everything named "iostream" in the header file to the include file. This makes the contents of the header file available in the code file.

In product quality C ++ code, header files are often used as a means of sharing code between translation units. The header file is a file inserted into the translation unit via the #include directive. Do not define an unnamed namespace in the header file. Defining an unnamed namespace in the header file can produce amazing results. Because of the default internal link, each translation unit defines a unique instance of a member of the unnamed namespace being used in that translation unit. This leads to unpredictable results, possibly causing the executable to bloat or to accidentally cause undefined behavior due to a single definition rule (ODR) violation.

Header file abstraction: Another abstract type of C ++ is the header file. For example, consider the pow () method in the math.h header file. Every time we need to calculate the number of power, we simply calculate the actual power of the digital without knowing the basic arithmetic function, even if the function POW math.h passed as a parameter Call header file () and digital. Abstraction can be easily implemented using two functions provided by the access specifier. For example, a member that defines an internal implementation can be marked private in the class. And important information that needs to be provided to the outside world can be marked as public. These public members are within the class so they can access private members.