What do .c and .h file extensions mean to C?

0 votes
asked Nov 8, 2009 by alex-mcp

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

These are two library files that I'm copying from CS50.net, and I'm wondering why they have two different extensions.

4 Answers

0 votes
answered Nov 8, 2009 by stefano-borini

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

.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.

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

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).

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.

in the .c you will therefore put

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

Now, assume you want to pass the following flags:

0: if the search is case sensitive

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


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.

0 votes
answered Nov 8, 2009 by hexium

The .c is the source file and .h is the header file.

0 votes
answered Nov 8, 2009 by arak
.c : 'C' source code
.h : Header file

Usually, the .c files contain the implementation, and .h files contain the "interface" of an implementation.

0 votes
answered Nov 2, 2016 by sam

suppose you have to open your created c program from command prompt or by using any IDE then if u have not saved your file by .c then it will be difficult for IDE to search and distinct your file from others which will delay the search. So .c tells the type of file and its source like .java and others

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter