How to customize the C language .h header file?

    In project practice, if a .c file program is too long, you can put the functions in the file in a header file separately, and then include the header file in .c. In the past two days, according to the book "The Science and Art of C Language" and the information on the Internet, I found that there are actually two ways of writing.

Writing method 1: The implementation of the function is included in the .h header file. This header file actually puts the functions together for easy management. There is only one .h file in this case.

Writing method 2: Only some function declarations are included in the .h header file, and there is no specific implementation. The implementation of the function is placed in another .c source file that does not contain main(), and then the source file and header file need to be linked. In this case, both .h files and .c files are included.

Then you can directly reference the custom header file like including <stdio.h>. If you want to use it in other projects, you can put it in the header library.

I have only implemented the first of the two ways of writing. It should be noted that .h needs to be placed in the same folder as the .c file that calls .h.

Specific examples are as follows:

The .h file contains the following functions

void hello(void)
{
	printf("hello world\n");
}

The .c file program is as follows:

#include <stdio.h>
#include "test.h"

intmain()
{
	hello();
	return 0;
}

——————

If the .h file contains many function files, it is actually better to include only function declarations in the header file, and each function implementation is placed in a separate .c source file. This way of thinking is clear, and for large-scale problems, the processing process is relatively standardized.

But it has not been implemented. There is no consistent statement on the Internet and in books for header files that only contain function declarations. . . But it feels like it should be possible!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815700&siteId=291194637