Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What files can be included in include in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what files can be included inC language", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what files can be included in C language"!

Text/Edward

As mentioned earlier, the #include preprocessing directive causes the contents of another file to be copied into the current.c file during preprocessing, resulting in an intermediate file. Normally, this intermediate file is stored in compiler memory and will not be output, but we can use the gcc command to store this file, even with " gcc -E ./ helloworld.c > a.out"command to preserve the content generated by preprocessing. As shown in Figure 1.

Figure 1 Preprocessed output file

In Figure 1, we simply include a header file written by ourselves. This header file has a label MAX defined by #define and a variable a. When we include this header file and compile, the compiler will replace all the relevant label definitions in the.c source file in the preprocessing stage of the compilation process. If the.h header file contains the definition of variables, The definition of the variable is copied intact to the.c source file. The above is all that the document contains. However,#include preprocessing can be much more than that if you dig deeper.

The inclusion of library functions and the inclusion of local header files We have also mentioned before that when using #include to include header files, there are different ways to include header files according to whether the included header files are library functions or local header files written by users themselves. If you include a library function with #include, it is included as follows: #include has no restrictions on filename, although by convention library files in standard libraries end with an.h suffix. The compiler finds the header files for library functions by looking up "a series of standard library paths" defined by the compiler. For example, on a typical UNIX/UNIX-like system, the C compiler looks in the/user/include/directory for the header files of library functions. If it is a local header file, it is included as follows: #include "filename" If the header file is placed in a directory with all source files, there is no need to explicitly indicate its path. If not in the same folder, both UNIX and Borland C compilers support a variant form of pathnames that use absolute addresses. For example: #include "../ src/usart/ usart.h"

Think about it again. Can #include in C only support header files that contain the.h suffix? In fact, other than the C language standard library file is written in accordance with the convention with the end of.h, user-defined header files do not have this provision. such as shown in figure 2.

Figure 2. C header file with random suffix names

As you can see in Figure 2, I have included a c.x header file in the b.c C source file. I don't know what the c.x suffix means. However, the strange thing is that when the compiler does preprocessing, it actually processes the contents of the c.x file normally. Except for VSCode when doing static code parsing, there is an error prompt, and nothing else seems to happen. Therefore, in a.c file with #include what suffix file, in the C language there is no specification. It's just that it's customary to use files ending in.h as header files. However, C language source file suffix name has clear requirements, must use.c suffix file.

The contents of the header file Next, the last content, what can be written in the header file? This problem is very mysterious, C language has no provisions, you can write it yourself. I've seen a buddy (a half-way engineer) define functions all in the header file. For such talents, I only say that he is talented, talented, and has excellent bones. C language header file inside can indeed be used to write functions, you did not hear wrong, I write again, can indeed be used to write functions. However, functions written this way are difficult to include in multiple other files. I didn't want to explain this problem because it was too destructive. I was afraid that this negative teaching material would be turned into a positive one. But to satisfy everyone's curiosity, it was posted, as shown in Figure 3. Figure 3 negative teaching material to this, I believe we have a deeper understanding of "C language include can contain what files", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report