yaylitzis
yaylitzis

Reputation: 5544

Diferrence between code.c and code.h

I am learning C and I have seen in some examples that code is saved with .h and I am not sure when should I save a code to .h. The files with ending .c contain main() and the files ending .h may contain declaration of global variables or functions (but not the main() )?

Upvotes: 0

Views: 149

Answers (7)

steveha
steveha

Reputation: 76725

At the simplest and most abstract level:

The .c file should contain the code that implements your functions.

The .h file should contain the "interface" to your code.

The .h file contains everything that other code would need to use the code in the .c file.

For example, if you have a function that adds two integers together it might look like this:

// in the .c file: the actual implementation of the function
int add_two_integers(int a, int b)
{
    return a + b;
}

// in the .h file: just the interface
int add_two_integers(int a, int b);

The .h file includes everything that some code would need to know in order to use the functions in the .c file. In this case there is just one function and the .h file contains its name and all the type information associated with it.

Another way to think about this: the .h file contains a "promise" that there is a function called add_two_integers() that you can use, and it tells how to use it. The code in the .c file might be compiled to a library file, and the C compiler won't look inside a library file and figure out what is there. It's up to you to just tell the C compiler what to expect, using a .h file.

In some sense the division between what goes in a .c file and what goes in a .h file is an implementation detail. It would be possible to make a system that scanned through a .c file twice, once to compile it and once to extract interface information. But in C, it is your job to split the two.

Other things that might go into a .h file: predefined constants, custom data types, macros, and inline functions. The macros and inline functions are logically code and thus should ideally be in the .c file, but as noted above, that's not the way things are implemented in C. The compiler needs to know about macros and inline functions, so if you want to share them, you share them in a .h file.

Upvotes: 2

Chinna
Chinna

Reputation: 4002

As your understanding, .h files contains declarations of global variables or functions. Below are the cases you need to consider,

--> Functions which are called by .c file are available as .o files. In this case .o files are linked with our executable during linking time and compiler don't know any thing about functions. so we need declarations of that functions. Then we make .h file which is having function declarations as a part.

--> Functions which are called by .c file having source code, In this case we generally make a .h file which contains function definitions and we include that file in .c file.

Upvotes: 2

jnix
jnix

Reputation: 480

If you want to use same code in two files instead of writting that code in two files.you can create common file which you can include in both files.such common code as well as common declarations can be put inside .h files.

and .c files will contain your uniquie logic and main method

Thanks

Upvotes: 0

uhs
uhs

Reputation: 848

Declarations (prototypes) go in .h files. The .h file is the interface to whatever is implemented in the corresponding .c file. Definitions go in .c files. They implement the interface specified in the .h file.

The difference is that a .h file can (and usually will) be #included into multiple compilation units (.c files). If you define a function in a .h file, it will end up in multiple .o files, and the linker will complain about a multiply defined symbol. That's why definitions should not go in .h files. (Inline functions are the exception.)

If a function is defined in a .c file, and you want to use it from other .c files, a declaration of that function needs to be available in each of those other .c files. That's why you put the declaration in a .h, and #include that in each of them. You could also repeat the declaration in each .c file, but that leads to lots of code duplication and an unmantainable mess.

If a function is defined in a .c file, but you don't want to use it from other .c files, there's no need to declare it in the header. It's essentially an implementation detail of that .c file. In that case, make the function static as well, so it doesn't conflict with identically-named functions in other files.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409364

Basically it is like this (very simplified):

In header files you have declarations, structures and preprocessor-macros that are needed by multiple source files.

In the source file you have actual implementation of functions, definitions of global variables, as well as declarations and preprocessor-macros private to that source file.

In C and many languages based on C (like C++), the process to compile a file is done in two steps: First the preprocessor runs, that creates a translation unit which is the combination of all included header files and the source file, which is passed to the compiler proper.

Upvotes: 2

Srikanth
Srikanth

Reputation: 445

.c files will contain function definitions & global variable declarations . where as .h will contain the function prototypes and other dependent library files .

Upvotes: 1

Julien
Julien

Reputation: 2584

The files ending with .h (headers) are made to be shared via #include.

The files ending with .c (bodies) are made for the implementation of headers and the rest of your code.

It's as simple as that :)

Upvotes: 2

Related Questions