什么.C和.h文件扩展名的意思是到C? [英] What do .c and .h file extensions mean to C?

查看:260
本文介绍了什么.C和.h文件扩展名的意思是到C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切都在标题;超级简单的我看,但它是如此难以寻找喜欢的东西随时随地语法的东西。

It's all in the title; super-simple I reckon, but it's so hard to search for syntactical things like things anywhere.

这是两个库文件,我从网上复制的,我不知道为什么他们是所谓的两个不同的名字。

These are two library files I'm copying from the web, and I'm wondering why they're called two different names

从这里开始: CS50.net

推荐答案

.C:C文件(其中实际行动,在一般情况)

.c : c file (where the real action is, in general)

.H:头文件(将包含在一个preprocessor ​​的#include 指令)。包含的东西,一般认为与code的其他部分共用,如函数原型,#define'd东西,全局变量extern声明(哦,恐怖)等等。

.h : header file (to be included with a preprocessor #include directive). Contains stuff that is normally deemed to be shared with other parts of your code, like function prototypes, #define'd stuff, extern declaration for global variables (oh, the horror) and the like.

从技术上讲,你可以把一切都在一个文件中。整整一个C程序。线条万元。但是,我们人类往往会整理东西。所以你创建不同的C文件,每个文件包含特定的功能。这是所有漂亮和干净。然后突然你意识到一个声明你在给定的C文件还应该存在于另一个C文件。所以,你会复制它们。因此,最好是提取声明,并把它放到一个共同文件,这是.H

Technically, you could put everything in a single file. A whole C program. million of lines. But we humans tend to organize things. So you create different C files, each one containing particular functions. That's all nice and clean. Then suddenly you realize that a declaration you have into a given C file should exist also in another C file. So you would duplicate them. The best is therefore to extract the declaration and put it into a common file, which is the .h

例如,在cs50.h你发现了什么叫你的函数前进的声明。
正向声明是一个快速的方法来告诉编译器函数应该怎么调用(如输入什么PARAMS),并返回什么,所以如果你调用一个函数与错误数量的参数可以进行适当的检查(例如,它会抱怨)。

For example, in the cs50.h you find what are called "forward declarations" of your functions. A forward declaration is a quick way to tell the compiler how a function should be called (e.g. what input params) and what it returns, so it can perform proper checking (for example if you call a function with the wrong number of parameters, it will complain).

又如。假设你写一个包含定期进行前pression匹配函数的.c文件。你想你的函数接受正规前pression,该字符串匹配,并且讲述了一个参数,如果比较有区分大小写。

Another example. Suppose you write a .c file containing a function performing regular expression matching. You want your function to accept the regular expression, the string to match, and a parameter that tells if the comparison has to be case insensitive.

在.C你会因此就把

bool matches(string regexp, string s, int flags) { the code }

现在,假设你想通过以下标志:

Now, assume you want to pass the following flags:

0:如果搜索是区分大小写

0: if the search is case sensitive

1:如果搜索不区分大小写

1: if the search is case insensitive

和你想保持自己开放的新标志,所以你没有把一个布尔值。
玩数字游戏是很难的,所以你这些标志定义了一个有用的名字

And you want to keep yourself open to new flags, so you did not put a boolean. playing with numbers is hard, so you define useful names for these flags

#define MATCH_CASE_SENSITIVE 0
#define MATCH_CASE_INSENSITIVE 1

此信息进入.H,因为如果任何程序要使用这些标签,就无法知道它们,除非你包括信息的方式。当然,你可以把它们放在.C,但你必须包含的.c code(整!),这是在浪费时间和麻烦的根源。

This info goes into the .h, because if any program wants to use these labels, it has no way of knowing them unless you include the info. Of course you can put them in the .c, but then you would have to include the .c code (whole!) which is a waste of time and a source of trouble.

这篇关于什么.C和.h文件扩展名的意思是到C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆