C – Strange C function syntax

Strange C function syntax… here is a solution to the problem.

Strange C function syntax

So far, I know my C. I saw this strange syntax when I looked at the source file for PHP I downloaded:

PHPAPI int php_printf(const char *format, ...)
{
     code...
}

What does PHPAPI do before returning type int? I’ve tried searching everywhere but I don’t understand what that means. Is it the second return type? This is not possible because the function does return an int. Maybe it extends to some other structure declared in the header file?

Solution

Difficult method:

Go to makefile and add the line to compile the source code: -E so you will see the source code after the preprocessing stage.

Easy way:

Search for PHPAPI:: in all projects

Find it in php.h:

#ifdef PHP_WIN32
#include "win95nt.h"
#   ifdef PHP_EXPORTS
#   define PHPAPI __declspec(dllexport) 
#   else
#   define PHPAPI __declspec(dllimport) 
#   endif
#define PHP_DIR_SEPARATOR '\\'
#else
#define PHPAPI
#define THREAD_LS
#define PHP_DIR_SEPARATOR '/'
#endif

Now you need to know what is __declspec (dllexport) and what is __declspec (dllimport).

In the SO thread – What is __declspec and when do I need to use it?

See also Alexander Gessler answer:

The canonical examples are __declspec(dllimport) and
__declspec(dllexport), which instruct the linker to import and
export (respectively) a symbol from or to a DLL.

// header
__declspec(dllimport) void foo();

 code - this calls foo() somewhere in a DLL
foo();

(__declspec(..) just wraps up Microsoft’s specific stuff – to
achieve compatibility, one would usually wrap it away with macros)

Related Problems and Solutions