Someone was recently reviewing a bit of code I am working on and told me that I should move some function declarations to the header file and call them static. Is the the preferred way of formatting c code and header files?
Per their suggestions my code now roughly looks like this:
header.h
# define USED_ONLY_IN_FILE1
# define USED_IN_BOTH
extern uint8_t var_used_in_both_files;
extern volatile uint8_t buffer_in_both_files[16];
static void funct_used_only_in_file1(void); //This used to live in file1.c without the static keyword
void funct_used_in_both_files(void);
file1.c
#include "header.h"
uint8_t var_used_in_both_files;
volatile uint8_t buffer_in_both_files[16];
void funct_used_only_in_file1(void)
{
var_used_in_both_files++;
}
void funct_used_in_both_files(void)
{
buffer_in_both_files[0] = 1;
}
file2.c
#include "header.h"
void funct_b (void)
{
var_used_in_both_files+=2;
funct_used_in_both_files();
}
It feels a little weird to me that I now have a function essentially being declared static
in both .c
files when header.h
is included. I'm still wrapping my head around keywords like static
, extern
, and volatile
so I'd really appreciate an explanation of why we would want a function that's used only in one file to be declared in a header file then marked static
. Why would we want this?