In C, when stating extern or static specifiers on a function, what is the proper syntax use –
only on declaration? on definition? both? is it the same with a variable?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Declaring a
externFunction:The keyword
externshould be used only while declaring(not defining) a function.Note that functions by default have external linkage so the keywordexternon a function declaration is redundant.Defining a
externFunction:The function definition should not be specified with
externkeyword. The definition could be in another cpp file.Declaring a
staticFunction:A
staticfunction restricts the use of the function to the Translation Unit in which it is declared. You need to specify the keyword when you declare it.Defining a
staticFunction:The function definition needs to be defined in the same TU.You don’t need to specify the
statickeyword while defining it.Using a
externvariable:You declare a variable as
externwhen you want to share the same global variable accross different translation Unit.You need to declare the variable with
externkeyword while you need to define it in one and only one cpp file.file1.h
file1.cpp
file2.cpp