Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a function prototype always be in its header file?

Lets say we have a few C source files such as file1.c, file2.c and main.c. We have functions as:

file1.c
      |---> file1Func1()
      |---> file1Func2()

file2.c
      |---> file2Func1()
      |---> file2Func2()

and the main file uses these functions. Now it would be natural that I create and add respective function prototype in header files file1.h and file2.h, then include these headers in main.c to use the functions.

What if I have a very large project with over thousand source (C) files, should I always create a header (then add function prototype) for every source file. Then include the header to use the functions?

Or using extern for using a function defined elsewhere (in some other source file) and rely on linker to search and fetch the function from the object file during link time?

Note: using the latter approach triggers MISRA warning of no function prototype.

like image 878
Osaid Avatar asked Mar 18 '16 04:03

Osaid


People also ask

Do function prototypes go to header file?

The function prototype is also called the function declaration (as opposed to the function definition, which includes the body of the function). You can also put function prototypes in header files, whose names end with . h, and then include them with stdio.

Where should a function prototype be placed?

Generally, the function prototype is placed after the header file in the program. The scope of the function prototype is considered within the same block as the function call.

Do header files contain only prototypes?

A header file contains macros, structure declaration and function prototypes.

What's the difference between a function prototype and a header?

The only difference between the function prototype and the function header is a semicolon (see diagram below). The function definition is placed AFTER the end of the int main(void) function.


2 Answers

All functions that are part of the interface, that is functions which is called by another module, should have function prototypes in the header file. Preferably together with comments documenting how that function should be used.

Functions that are not part of the interface and only used internally within the file should not have a prototype in the header. For such functions, declare the prototype at the top of the c file, and declare it as static.

This is how all (professional) C programs are written. As a side-note, this sound design is also required by MISRA-C.

There should never be a reason for you to use the extern keyword for functions. Note that a function prototype like

void func (void);

is completely equivalent to

extern void func (void);

If you need to use a function, include the relevant header.

like image 62
Lundin Avatar answered Sep 23 '22 06:09

Lundin


What if I have a very large project with over thousand source (c) files, should I always create a header (then add function prototype) for every source file. Then include the header to use the functions?

The short answer is "Yes".

The slightly longer answer is "Yes but you may omit functions from header files that are implementation details of other functions in a source file".

Declaring functions in header files and #includeing the header files makes sure that function definitions and function calls stay in sync. Otherwise, it is easy to make mistakes and those mistakes are caught at link time instead of at compile time.

like image 42
R Sahu Avatar answered Sep 22 '22 06:09

R Sahu