函数声明文件中需要C头指令吗?

时间:2022-09-08 19:31:54

I am in the process of exploring the C programming language and have started to learn about header files.

我正在探索C编程语言,并开始学习头文件。

I am working with three different files in my program. One header file and two source files:

我在我的程序中使用三个不同的文件。一个头文件和两个源文件:

encrypt.h

void encrypt(char *message);

encrypt.c

#include "encrypt.h"

void encrypt(char *message)
{    
    while(*message)
    {
        *message = *message ^ 31;
        message++;
    }   
}

main.c

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

int main() 
{
    char myMessage[] = "Hello";

    printf("Regular message: %s\n", myMessage);
    encrypt(myMessage);
    printf("Encrypted message: %s\n", myMessage);        

    return 0;
}

The first line in the encrypt.c file is the #include statement:

encrypt.c文件中的第一行是#include语句:

#include "encrypt.h"

My question is why is this line necessary inside of this file. I was reading how the #include directive has a copy/paste functionality and inside of the main.c file I can see how this approach would support forward declaration for the encrypt function; however, inside of the encrypt.c file, it would seem that forward declaration of the function is not necessary, and was wondering what was the purpose of using the include directive in this situation?

我的问题是为什么这个行必须在这个文件中。我正在阅读#include指令如何具有复制/粘贴功能,并且在main.c文件中我可以看到这种方法如何支持加密函数的前向声明;但是,在encrypt.c文件中,看起来函数的前向声明似乎没有必要,并且想知道在这种情况下使用include指令的目的是什么?

I apologize if this question has been asked elsewhere and will mark as duplicate as required. Thank you for the clarification.

如果在其他地方已经问过这个问题我会道歉,并且会根据需要标记为重复。谢谢你的澄清。

2 个解决方案

#1


3  

In your particular case, it is not necessary for encrypt.c to include encrypt.h, but it is conventional for it to do so.

在您的特定情况下,encrypt.c不必包含encrypt.h,但它是常规的。

The normal purpose of header files is to provide a common location for declarations that can be shared among multiple source files. This avoids inconsistent declarations in different parts of the program, and when changes need to be made, it may reduce the number of files that need to be updated.

头文件的正常用途是为可以在多个源文件之间共享的声明提供公共位置。这避免了程序的不同部分中的不一致声明,并且当需要进行更改时,它可以减少需要更新的文件的数量。

encrypt.c does not need to include encrypt.h because the latter contains only a declaration of function encrypt(), and encrypt.c already has one in the form of a definition of that function. It is nevertheless useful, because it allows the compiler to verify that the declaration in encrypt.h is consistent with the definition in encrypt.c.

encrypt.c不需要包含encrypt.h,因为后者只包含函数encrypt()的声明,而encrypt.c已经有一个以该函数定义的形式。它仍然有用,因为它允许编译器验证encrypt.h中的声明是否与encrypt.c中的定义一致。

If encrypt.c defined more functions, and encrypt.h provided declarations for all of them, then it could also serve in encrypt.c to ensure that declarations of all functions therein were visible to all functions therein, thus overcoming any dependency on the relative order of the actual definitions.

如果encrypt.c定义了更多的函数,并且encrypt.h为所有函数提供了声明,那么它也可以在encrypt.c中服务,以确保其中所有函数的声明对其中的所有函数都是可见的,从而克服对相对的所有依赖性。实际定义的顺序。

#2


0  

Side note:

The moment you add a new source file fancy_encrypt.c which calls the encrypt() function you must have a compile guard in the encrypt.hfile:

添加一个调用encrypt()函数的新源文件fancy_encrypt.c的那一刻,你必须在encrypt.hfile中有一个编译保护:

#ifndef ENCRYPT_H
#define ENCRYPT.H

void encrypt(char * message);

#endif

Otherwise you will get a compiler error due to multiple declarations of encrypt().

否则,由于encrypt()的多个声明,您将收到编译器错误。

#1


3  

In your particular case, it is not necessary for encrypt.c to include encrypt.h, but it is conventional for it to do so.

在您的特定情况下,encrypt.c不必包含encrypt.h,但它是常规的。

The normal purpose of header files is to provide a common location for declarations that can be shared among multiple source files. This avoids inconsistent declarations in different parts of the program, and when changes need to be made, it may reduce the number of files that need to be updated.

头文件的正常用途是为可以在多个源文件之间共享的声明提供公共位置。这避免了程序的不同部分中的不一致声明,并且当需要进行更改时,它可以减少需要更新的文件的数量。

encrypt.c does not need to include encrypt.h because the latter contains only a declaration of function encrypt(), and encrypt.c already has one in the form of a definition of that function. It is nevertheless useful, because it allows the compiler to verify that the declaration in encrypt.h is consistent with the definition in encrypt.c.

encrypt.c不需要包含encrypt.h,因为后者只包含函数encrypt()的声明,而encrypt.c已经有一个以该函数定义的形式。它仍然有用,因为它允许编译器验证encrypt.h中的声明是否与encrypt.c中的定义一致。

If encrypt.c defined more functions, and encrypt.h provided declarations for all of them, then it could also serve in encrypt.c to ensure that declarations of all functions therein were visible to all functions therein, thus overcoming any dependency on the relative order of the actual definitions.

如果encrypt.c定义了更多的函数,并且encrypt.h为所有函数提供了声明,那么它也可以在encrypt.c中服务,以确保其中所有函数的声明对其中的所有函数都是可见的,从而克服对相对的所有依赖性。实际定义的顺序。

#2


0  

Side note:

The moment you add a new source file fancy_encrypt.c which calls the encrypt() function you must have a compile guard in the encrypt.hfile:

添加一个调用encrypt()函数的新源文件fancy_encrypt.c的那一刻,你必须在encrypt.hfile中有一个编译保护:

#ifndef ENCRYPT_H
#define ENCRYPT.H

void encrypt(char * message);

#endif

Otherwise you will get a compiler error due to multiple declarations of encrypt().

否则,由于encrypt()的多个声明,您将收到编译器错误。