Electronic – extern and the XC8 C compiler

mplabxpicxc8

Perhaps I don't fully understand extern. With the XC8 C compiler, it seems like I can get away without using it.

In the file keypad.c I have a function signed char keypadGetPressedKeyLabel(void). In the header file keypad.h I have:

signed char keypadGetPressedKeyLabel(void);

I thought I would have to use extern, ie:

extern signed char keypadGetPressedKeyLabel(void);

The project builds without extern. And it works. Might this due to the fact that keypad.c and keypad.h are all part of the project? I'm using MPLABX.

Best Answer

extern is not strictly necessary for function prototypes in .h files - whether or not the function is actually used in one or many different .c files in your project.
Whether you have extern int foo(void); or just int foo(void); in your .h your compiler will read is as a function prototype either way.

Its necessary for global variables though since unlike functions they don't have prototypes.
So if you have an int thing; in one .c file and you want to use it in another .c file then you'll need an extern int thing; to tell the compiler that it does exist 'somewhere' in your project.