0

I'm trying to read a project documentation. Under the title Prototype, I'm having this code :

VisionAPI_RETURN VisionAPI VisionInterf_ImageAttach( 
     VisionAPI_HANDLE ImageHandle , 
     uint32_t NumImages 
);

The project's interfaces will be build in C, when the functions will be in C++. When reading the documentation, I read prototype, so I supposed it is "functions prototypes". When reading more, I find the author sometimes using the term "function" and sometimes "interface". I know how to make an interface in C++ but not in C.

So is the code above about interface or function prototype?

Otherwise, how to create an interface in C language? Is it by creating structure (struct) ?

I hope my question is not stupid. I'm a Java developer, and a C/C++ simple student.

0

3 Answers 3

2

This declares the function, without providing an implementation (the definition). So, you could say this is an interface. You just tell the compiler: "Hey, there is a function with the name VisionInterf_ImageAttach" so it doesn't complain when you call it. More about declaration vs. definition.

This, of course depends on what VisionAPI_RETURN and VisionAPI resolve to, assuming they are macros.

2
  • I'm supposing to have only "one returned type" and here I'm having : VisionAPI_RETURN VisionAPI, how should I interpret this? Thanks
    – loulou
    Sep 10, 2013 at 12:01
  • 1
    @user1847726: I highly suspect that both (or at least one) are #define macros and resolve to something different. Try finding out where they are defined or have a look at the preprocessed file (gcc -E).
    – bitmask
    Sep 10, 2013 at 14:19
2

There is no such thing in C language as an interface: in addition to primitive types and pointers, the language supports structs and functions; that's all you have to work with.

Your code shows an example of a forward declaration of a function. Interfaces to C modules consist of multiple such declarations, along with declaration of structs that these functions use.

1
  • +1. Thank you sir. for the affirmations you gave me about language C. That save time from looking for something that doesn't exist.
    – loulou
    Sep 12, 2013 at 9:31
1

(Adding to the already useful info given by @bitmask and @dasblinkenlight)

In C a function prototype is, roughly, the declaration of a function signature, i.e. the declaration of the function name, return type and parameter list types (i.e. the types of the parameters the function accepts, in their respective order).

Therefore, in a sense, the prototype of a function can be viewed as the interface of the function towards client code (in this case the term interface is used in a general way, unlike what it means in OOP and Java in particular).

As a simpler example, suppose you defined a function like this:

int MyFunc( double x, char * z )
{
   // function body code
}

then its prototype would be:

int MyFunc( double x, char * z );

or even

int MyFunc( double, char * );

since parameter names are optional in function prototypes.

There is no mechanism in C for creating an equivalent replica to Java interfaces, although often programmers refer to specially crafted C header files as "interfaces". These header files are filled with function prototypes and other declarations that represent the "interface" of the library they belong toward client code.

Therefore client code can #include those header files to access the facilities offered by the library without knowing its implementation (here is the "interface" thing), which usually is resolved at link time (or at run-time if dynamic linking is involved).

Edit (to answer a comment)

What you see before function name is probably some macro trick. Since I don't know what's the context I can only guess. The two identifiers VisionAPI_RETURN VisionAPI are most probably defined as macros. From their names I guess that the first expands to the actual return type, while the second could be either an empty macro (usually used to mark some category of declarations) or expands to some compiler-specific keyword combination, usually used to declare the function low-level calling convention.

For example, somewhere there could be these macro definitions:

#define VisionAPI_RETURN int
#define VisionAPI 

so that, after the preprocessor has finished its work, the compiler will see this:

int VisionInterf_ImageAttach( 
     VisionAPI_HANDLE ImageHandle, 
     uint32_t NumImages 
);

Another example - if those macros are defined like this:

#define VisionAPI_RETURN struct MyReturn
#define VisionAPI __stdcall

then the prototype will be expanded like this:

struct MyReturn __stdcall VisionInterf_ImageAttach( 
     VisionAPI_HANDLE ImageHandle, 
     uint32_t NumImages 
);

In this case the return type would be struct MyReturn, while the __stdcall keyword would indicate the "stdcall" calling convention (the __stdcall keyword is used by Microsoft compilers). This resulting syntax is an extension to standard C syntax (many compilers have some kind of such extensions).

1
  • Hi! Please, what would be the return type in my case? VisionAPI_RETURN or VisionAPI? and What would be the other? Thanks!
    – loulou
    Sep 10, 2013 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.