C Header 在语言上是什么?
What is C Header Linguistically?
我想知道C语言中的"Header"是什么意思?是吗:
- 代码中的区域,例如 HTML 代码中的 header 区域(标题> 或声明区域?或定义区域?,或类似)?
- 或者它就像一个可以调用的函数(可以是:函数,或子例程,或范围,或类似的)。
通常在 C 中谈论 "headers" 时,指的是包含在 the #include
preprocessor directive 中的文件。这些被称为 header 文件。
它也可以是其他东西,比如源代码(或 header 文件)顶部的评论可以是 header。函数前的注释可以是函数 header.
或者在通过 Internet 读取数据文件或数据时,许多协议将数据拆分为 header 和实际数据(参见 HTTP).
a header 是一个扩展名为 .h 的文件。
它旨在帮助编译器理解代码中使用的符号(方法、函数、结构、变量...)。将其视为书末的词汇表。
它仅用于开发目的。
它可以帮助开发人员了解哪些功能可用,而无需查看所有的实现文件。就像手册页一样。
在 google 上搜索? http://www.tutorialspoint.com/cprogramming/c_header_files.htm:)
What is C header Linguistically?
语言学 我会说 C header 文件描述了翻译单元提供的接口,即随附的 C 文件。
"接口" 通过
- 类型
- 常量
- (全局)变量
- 函数
并非以上所有内容都需要成为 C header 的一部分。
C header 从 C 语言的角度来看是可选的,但按照惯例它们应该存在。
A header是C程序员普遍接受的约定。
它通常是一个包含在 C 源文件中的 .h 文件,它提供了几个好处。
1.- 提供数据类型、全局变量、常量和函数的声明。
因此您不必一次又一次地重写它们。如果需要更改它们,您只需在单个文件中进行更改。
在示例中,这个由两个编译单元(两个 .c 文件)组成的程序编译并运行得很好。
// File funcs.c
#include <stdio.h>
struct Position {
int x;
int y;
};
void print( struct Position p ) {
printf("%d,%d\n", p.x, p.y );
}
// File main.c
struct Position {
int x;
int y;
};
int main(void) {
struct Position p;
p.x = 1; p.y = 2;
print(p);
}
但是将 struct Position
的声明放在 header 文件中并在需要的地方 #include
更易于维护,就像这样:
// File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
struct Position {
int x;
int y;
};
#endif // FUNCS_H
//File funcs.c
#include <stdio.h>
#include "funcs.h"
void print( struct Position p ) {
printf("%d,%d\n", p.x, p.y );
}
//File main.c
#include "funcs.h"
int main(void) {
struct Position p;
p.x = 1; p.y = 2;
print(p);
2.- 提供一些类型安全。
C 特征 implicit declaration of functions。 "feature"(或者更确切地说是一个有争议的语言设计错误)已在 C99 中修复。
考虑这个由两个 .c 文件组成的程序:
//File funcs.c
#include<stdio.h>
int f(long n)
{
n = n+1;
printf("%ld\n", n );
}
// File main.c
int main(void)
{
f("a");
return 0;
}
使用 gcc 这个程序编译时没有警告或错误。但不符合我们的合理预期和期望:
jose@cpu ~/t/tt $ gcc -o test *.c
jose@cpu ~/t/tt $ ./test
4195930
像这样使用 header 文件 :
//File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
int f(long n);
#endif // FUNCS_H
//File funcs.c
#include<stdio.h>
int f(long n) {
n = n+1;
printf("%ld\n", n );
}
// File main.c
#include"funcs.h"
int main(void) {
f("a");
return 0;
}
程序仍然编译并运行错误,但至少我们得到了警告:
jose@cpu ~/t $ gcc -o test *.c
main.c: In function 'main':
main.c:5:5: warning: passing argument 1 of 'f' makes integer from pointer without a cast
f("a");
^
In file included from main.c:2:0:
funcs.h:3:5: note: expected 'long int' but argument is of type 'char *'
int f(long n);
^
jose@cpu ~/t $ ./test
4195930
3.- 提供一个 public 接口,同时隐藏实现细节。
当您设计程序时,最好将其模块化。那就是保证它的不同部分(模块)尽可能的独立。这样当您需要对一个模块进行更改时,您不必担心这种更改会影响其他模块
Headers 有助于做到这一点,因为您在模块的 header 中放入了此类模块的用户将需要的数据结构、函数原型和常量。
实现细节进入 .c 文件。
图书馆就是这样运作的。 API 接口在 header 文件中指定和分发。但是 API 代码在不需要分发的 .c 文件中。作为图书馆的用户,您只需要 headers 和编译后的图书馆,而不是它的源代码。
我想知道C语言中的"Header"是什么意思?是吗:
- 代码中的区域,例如 HTML 代码中的 header 区域(标题> 或声明区域?或定义区域?,或类似)?
- 或者它就像一个可以调用的函数(可以是:函数,或子例程,或范围,或类似的)。
通常在 C 中谈论 "headers" 时,指的是包含在 the #include
preprocessor directive 中的文件。这些被称为 header 文件。
它也可以是其他东西,比如源代码(或 header 文件)顶部的评论可以是 header。函数前的注释可以是函数 header.
或者在通过 Internet 读取数据文件或数据时,许多协议将数据拆分为 header 和实际数据(参见 HTTP).
a header 是一个扩展名为 .h 的文件。 它旨在帮助编译器理解代码中使用的符号(方法、函数、结构、变量...)。将其视为书末的词汇表。 它仅用于开发目的。 它可以帮助开发人员了解哪些功能可用,而无需查看所有的实现文件。就像手册页一样。
在 google 上搜索? http://www.tutorialspoint.com/cprogramming/c_header_files.htm:)
What is C header Linguistically?
语言学 我会说 C header 文件描述了翻译单元提供的接口,即随附的 C 文件。
"接口" 通过
- 类型
- 常量
- (全局)变量
- 函数
并非以上所有内容都需要成为 C header 的一部分。
C header 从 C 语言的角度来看是可选的,但按照惯例它们应该存在。
A header是C程序员普遍接受的约定。
它通常是一个包含在 C 源文件中的 .h 文件,它提供了几个好处。
1.- 提供数据类型、全局变量、常量和函数的声明。
因此您不必一次又一次地重写它们。如果需要更改它们,您只需在单个文件中进行更改。
在示例中,这个由两个编译单元(两个 .c 文件)组成的程序编译并运行得很好。
// File funcs.c
#include <stdio.h>
struct Position {
int x;
int y;
};
void print( struct Position p ) {
printf("%d,%d\n", p.x, p.y );
}
// File main.c
struct Position {
int x;
int y;
};
int main(void) {
struct Position p;
p.x = 1; p.y = 2;
print(p);
}
但是将 struct Position
的声明放在 header 文件中并在需要的地方 #include
更易于维护,就像这样:
// File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
struct Position {
int x;
int y;
};
#endif // FUNCS_H
//File funcs.c
#include <stdio.h>
#include "funcs.h"
void print( struct Position p ) {
printf("%d,%d\n", p.x, p.y );
}
//File main.c
#include "funcs.h"
int main(void) {
struct Position p;
p.x = 1; p.y = 2;
print(p);
2.- 提供一些类型安全。
C 特征 implicit declaration of functions。 "feature"(或者更确切地说是一个有争议的语言设计错误)已在 C99 中修复。
考虑这个由两个 .c 文件组成的程序:
//File funcs.c
#include<stdio.h>
int f(long n)
{
n = n+1;
printf("%ld\n", n );
}
// File main.c
int main(void)
{
f("a");
return 0;
}
使用 gcc 这个程序编译时没有警告或错误。但不符合我们的合理预期和期望:
jose@cpu ~/t/tt $ gcc -o test *.c
jose@cpu ~/t/tt $ ./test
4195930
像这样使用 header 文件 :
//File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
int f(long n);
#endif // FUNCS_H
//File funcs.c
#include<stdio.h>
int f(long n) {
n = n+1;
printf("%ld\n", n );
}
// File main.c
#include"funcs.h"
int main(void) {
f("a");
return 0;
}
程序仍然编译并运行错误,但至少我们得到了警告:
jose@cpu ~/t $ gcc -o test *.c
main.c: In function 'main':
main.c:5:5: warning: passing argument 1 of 'f' makes integer from pointer without a cast
f("a");
^
In file included from main.c:2:0:
funcs.h:3:5: note: expected 'long int' but argument is of type 'char *'
int f(long n);
^
jose@cpu ~/t $ ./test
4195930
3.- 提供一个 public 接口,同时隐藏实现细节。
当您设计程序时,最好将其模块化。那就是保证它的不同部分(模块)尽可能的独立。这样当您需要对一个模块进行更改时,您不必担心这种更改会影响其他模块
Headers 有助于做到这一点,因为您在模块的 header 中放入了此类模块的用户将需要的数据结构、函数原型和常量。
实现细节进入 .c 文件。
图书馆就是这样运作的。 API 接口在 header 文件中指定和分发。但是 API 代码在不需要分发的 .c 文件中。作为图书馆的用户,您只需要 headers 和编译后的图书馆,而不是它的源代码。