Define a function before main?

c c99
51,040

Solution 1

How and where to prototype and define a function in C :

  1. Your function is used only in a specific .c file : Define it static in the .c file. The function will only be visible and compiled for this file.

  2. Your function is used in multiple .c files : Choose an appropriate c file to host your definition (All foo related functions in a foo.c file for example), and have a related header file to have all non-static (think public) functions prototyped. The function will be compiled only once, but visible to any file that includes the header files. Everything will be put together at link time. Possible improvement : always make the related header file, the first one included in its c file, this way, you will be sure that any file can include it safely without the need of other includes to make it work, reference : Large Scale C++ projects (Most of the rules apply to C too).

  3. Your function is inlinable (are you sure it is ?) : Define the function static inline in an appropriate header file. The compiler should replace any call to your function by the definition if it is possible (think macro-like).

The notion of before-after another function (your main function) in c is only a matter of style. Either you do :

static int foo(int foo) 
{ 
// code 
return 1; 
} 

int main(void) 
{ 
foo(1); 
return 0; 
} 

Or

static int foo(int foo);

int main(void) 
{ 
foo(1); 
return 0; 
} 

static int foo(int foo)
{ 
// code 
return 1; 
} 

will result in the same program. The second way is prefered by programmers because you don`t have to reorganize or declare new prototypes every time you declare a new function that use the other ones. Plus you get a nice list of every functions declared in your file. It makes life easier in the long run for you and your team.

Solution 2

People typically do it because it's easier to do with multiple files. If you declare in a header then you can just #include that header anywhere you need those functions. If you define them in a header and then include in another translation unit, bang.

Solution 3

Function declarations are required in C99. Function prototypes are not required in C99.

Declaring functions before the point of the call and defining them after the point of the call is a popular approach to structuring the program code. However, this is in no way what the "most" programmers do. On the contrary, a more popular approach is to define function before the point of the first call, in which case the separate declaration is not necessary. This approach requires less maintenance, which is why it is more popular than what you describe.

Separate declarations/definitions are normally used with external functions only, i.e. with functions used across several translation units. Such functions are declared in header files and defined in implementation files.

Solution 4

You should only ever define inline functions in headers. Although you can have extern inline functions, the common case is static inline.

Rule of thumb for header files:

  • function declarations should be extern
  • function definitions should be static inline
  • variable declarations should be extern
  • variable definitions should be static const

As C. Ross asked for it, here's reasoning behind it: A resource with external linkage should only ever be defined once[1]. It follows that definitions should not reside in header files, which are intended to be included in more than one place.

Having static definitions in header files won't lead to any problems, but is generally frowned upon because the code has to be compiled more than once and will be present in different object files, which will increase the executable size (assuming the linker isn't smart enough to figure out the code duplication).

The common exceptions to this rule are constants and inline functions, which are supposed to be visible to the compiler in each translation unit to make further optimizations possible.

Note: [1] This does not apply to inline functions with external linkage, but as it's unspecified which of the multiple definitions of an inline function will be used in the evaluation of a function designator, they are mostly useless

Solution 5

Your approach is fine for small programs. Header files are meant for declarations and constant definitions - they provide an interface to the program they "encapsulate". Headers are meant as an interface for other program units.

In case you have more .c files, forward declarations and header files are necessary, because a C function can be defined only once for the whole program (search for one definition rule), even though you may use the function anywhere (in any .c file). If you defined it in a header, it would get included in all .c files you use it in and result in multiple definitions.

Share:
51,040
Neeladri Vishweswaran
Author by

Neeladri Vishweswaran

I am a student at the Indian Institute of Science.

Updated on February 20, 2020

Comments

  • Neeladri Vishweswaran
    Neeladri Vishweswaran over 4 years

    Are function declarations/prototypes necessary in C99 ?

    I am currently defining my functions in a header file and #include-ING it in the main file. Is this OK in C99 ?

    Why do most programmers declare/prototype the function before main() and define it after main() ? Isn't it just easier to define them before main and avoid all the declarations/prototypes ?

    Contents of header.h file:

    int foo(int foo)
    {
    // code
    return 1;
    }
    

    Contents of main file:

    #include <stdio.h>
    
    #include "header.h"
    
    int main(void)
    {
    foo(1);
    return 0;
    }