Safe C Guidelines II: Header Files

I find the way of structuring modules and header files detailed here an important part of writing safe C. You can read more Safe C Guidelines here

One header file per module

Split the project up into modules, each module has a header file that declares its functions that can be called from other modules, and its variables that are accessible globally. There is only one file that contains these declarations and is included wherever this module may be used.

I prefix the variable name with the name of the module. This prevents variable name clashes.

The header file contains entries like

extern uint8_t  encoder_size;

extern void encoder_encode(uint8_t *data, uint8_t type);

In the file encoder.c, the top of the file will contain definitions of all variables. This is where memory for these variables is allocated. Variables that are only visible within the file will be defined with static and may not contain the module prefix encoder_.

unint8_t encoder_size;
uint32_t number_of_encode_calls;

void simple_encode(uint8_t *data){

void complex_encode(uint8_t *data){

void encoder_encode(uint8_t *data, uint8_t type){

    if( 1 == type){


   } else {




In function definitions in header files leave the name of variables

In function definitions in header files, leave the name of variables in there. If you have chosen descriptive variable names, as you should, it provides more hint to other programmers and you (when you come back to the code a year later), as to what the function does.

Always declare size of arrays

C allows arrays to be linked against without specifying their size. However declaring the size of the array is helpful to people writing code.

So use

uint8_t    data[100];

instead of

uint8_t data[];

Top of Safe C II

Need More Help? Have An Opinion?

Do you need more help to solve your problem? Would you like to ask the author a question about your specific problem? Do you have a great idea about this?

We will post an answer within 2 business days. If you need more immediate assistance or you would like to discuss your issue privately, please use our contact us form or call us at 1-888-215-8557. We love solving technical issues and there is no charge if we solve your problem over email or over a short phone call.

[ ? ]

Author Information (optional)

To receive credit as the author, enter your information below.

(first or full name)

(e.g., City, State, Country)

Submit Your Contribution

  •  submission guidelines.

(You can preview and edit on the next page)