If the C declarations match the definition, include keywords and qualifiers such as "static", "inline", etc.

Consider this example of a function declaration and definition (in the same translation unit):

inline static int foo(int x);

...

int foo(int x)
{
  return x+1;
}

      

I know the types must match, but what about other keywords and qualifiers? Should it inline static

be in both cases? Or just an ad?

And what part of the C standard or coding guideline can I use to substantiate the answer?

+3


source to share


4 answers


No, for inline

in particular, they don't have to be the same.

But this example is wrong from the start. For inline

you need a definition (whole function) with inline

in a .h file. This way inline

you avoid the character being defined in multiple translation units (.c) where you include the .h header.



Then, for exactly one translation unit, you expose only the declaration without inline

to indicate that the symbol should be generated in the corresponding .o object file.

+2


source


I want to provide details about the builtins,

you can separate the declaration and definition, but that definition must be available in every translation unit that uses functions, i.e. in your caseinline static int foo(int x);

Built-in functions are included in the ISO C99 standard, but there are currently significant differences between what GCC implements and what the ISO C99 standard is.

To declare an inline function, use the inline keyword in your declaration, for example:



static inline int
inc (int *a)
{
  return (*a)++;
}

      

Built-in function works as fast as a macro

Note that certain definitions in a function definition may make it unusable for inline substitution.

Note that in C, unlike C ++, the inline keyword does not affect function binding.

+1


source


Yes. inline and static. For example, the line of code for a function should be the same in the .h file where you declare and the .c file you define (yes, yes, in both cases), but in your main code.c file it should have the function name when invocation, so "foo (parameters passed)".

Hope this helps!

0


source


You only need an inline static

ad. You can leave it outside the function definition.

7.1.1 / 6:

A name declared in the scope of a namespace without a storage class specifier is externally related, unless it is internally related due to a previous declaration and provided that it is not declared const. Objects declared const and not explicitly declared extern have internal linkage.

-1


source







All Articles