text C中的头文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text C中的头文件相关的知识,希望对你有一定的参考价值。

*.h is called header files, are files that contain codes that you can share accross different programs.

We can make up our own header files. For example, we start a new file and give it a name of "struct.h".

In this "struct.h" file, we will be using "Struct" to define our own data structer.

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.

Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.

Include Syntax
Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms −

#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code.

#include "file"
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the current file. You can prepend directories to this list with the -I option while compiling your source code.

Example of header files in "struct0.c":
#include <cs50.h>
#include <stdio.h>
#include <string.h>

#include "struct.h"

以上是关于text C中的头文件的主要内容,如果未能解决你的问题,请参考以下文章

解释c中的头文件路径

C++中CTime的头文件

C 标准库中的头文件可以包含另一个头文件吗?

何时将静态函数定义放在 C 的头文件中?

将 Objective C 框架导入我的 Swift 项目后,我的项目无法识别桥接文件中的头文件

Code::Blocks 中的头文件和源文件问题