Extern on function prototypes?

my_math.h

// case 1 
unsigned int add_two_numbers(unsigned char a, unsigned char b);

//case 2 
extern unsigned int add_two_numbers(unsigned char a, unsigned char b); 

      

What is the difference between case 1 and case 2? I've never used extern for function prototypes, but looked at someone else's code (who's more experienced than me). I see that extern is always used when declaring a function prototype. Can anyone please point out the difference? (or point me to a link where I can find specific information). Google says this is due to external communication. Can someone point me to an example where one might work and another might not?

I am using inline C (KEIL) if that matters.

+3


source to share


3 answers


extern

is the binding specifier for the global binding. Its counterpart static

, which defines a file-local link. Since global binding is the default in C, adding extern

to a declaration has no meaning for a function declaration
. For a variable, it prevents automatic memory allocation, and using it is the only way to simply declare the variable in the global scope.



If you just put the Google keyword, you will find many articles for example. this one: geeks for geeks

+4


source


I learned the following variables years ago from an experienced programmer:

glo.h:
#ifndef EXTERN
#define EXTERN extern
#endif
...
EXTERN int gMyVar;
...

main.c:
#define EXTERN
#include "glo.h"

      



"glo.h" anywhere, just declares all global variables. "glo.h" included in main.c will allocate storage for variables. I believe this method was common practice.

0


source


For a function (not inline

) it doesn't make any difference, extern

implicitly, unless a storage class specifier is specified (note that this only applies to functions, objects are different), so it's just a matter of the style you are using.

I've seen both (never use extern

for functions / only use it for declarations in the header), maybe some use extern

for symmetry with object ids or to make grep easier for external characters.

Pick whichever you prefer and stay consistent, it doesn't matter.

0


source







All Articles