Handmade Hero»Forums»Code
Adrian
46 posts
None
C function prefixes
Edited by Adrian on
A C function looks like this:

1
2
3
4
return_type function_name(argument1, argument2, ...) 
{
  function body
}


However there are some prefixes you can attach between return_type and function_name called storage classes.
Are static and extern the only storage classes?

Are WINAPI and CALLBACK also storage classes? If not what are they? What is this syntax? Can I make my own whatever they are?
I read that these are calling conventions but if they are no storage classes than this is a different syntax right?

I'm sorry for asking this here but I didn't find anything online which helped me.
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
C function prefixes
Technically there are two things that can decorate a function, generally called "declaration specifiers". In addition to the storage class specifier (static, extern) there is also a "function specifier", which amusingly can only consist of "inline" or nothing. Don't ask me, I don't write the spec :)

The other things you see going on there, like calling conventions and whatnot, are technically compiler-specific extensions. Typically things like WINAPI are actually #define'd to be a compiler-specific double-underscore string which specifies something about the linkage or way the function is processed, such as __declspec(dllimport) or __stdcall. These are completely specific to the compiler, as far as I know, and have never been defined in the C spec (maybe they're defined in the C++ spec, since it's massive, but I don't read that spec so I have no idea :P )

The reason this sometimes _looks_ like it's a language feature and not a compiler-specific extension is because MSVC/GCC/LLVM seem to have converged on using similar syntaxes for some of these things, but again, as far as I know, they're actually completely separate from the language, and exist only because the compiler has added them.

Since they are added by the compiler, that means they don't appear in the spec, which means they don't appear in the official grammar, which means they don't have an official designation either (like "function specifier" or "storage-class specifier"), so I'm not sure if there's a right or wrong thing to call them. Since __declspec is often the macro name used to invoke them, that would suggest that the compiler vendors are not distinguishing below the declaration specifier level, which is just the bag of random stuff that exists to specify the function :)

- Casey
39 posts / 1 project
C function prefixes
cmuratori
These are completely specific to the compiler, as far as I know, and have never been defined in the C spec (maybe they're defined in the C++ spec, since it's massive, but I don't read that spec so I have no idea :P )
I knew it was in here somewhere
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
C function prefixes
Yep, that does not surprise me :) The C spec, which is more sane and can be read by humans, doesn't have those.

- Casey