[C/C++] func() vs. func(void)

Language/C&C++ 2011.07.27 09:12

In C, func() and func(void) have different function signature.
(But, in C++, these two are same.)

'func()' means 'this function can have any number of arguments (0 ~ infinite)'.
But, 'func(void)' means 'this function doesn't have argument.'
See following example.

#ifdef CASE1
void func(void); /* (1) */
void func();     /* (2) */

func(int i) {
        ; /* do something */

If 'CASE1' is defined, compiling this file complains error like "error: conflicting types for 'func'".
But, 'CASE1' is not defined, this is well-compiled.
Now, you can clearly understand difference.

So, using 'func(void)' is better for readibility if 'func' really doesn't have any arguement, instead of just 'func()'.

*** One more. ***
In C, 'extern' for function is default visibility.
So, in function declaration, 'extern void func(void);' is exactly same with 'void func(void);'.
Therefore any of them is OK.
( [ omitting 'extern' for simplicity ] vs. [ using 'extern' to increase readability ] )
But, default visibility of function depends on compiler.
For portability reason, using macro is usually better instead of using explicit directive - especially at shared library header.
(Ex. 'EXTERN void func(void)')

tags :
Trackback 0 : Comment 0

티스토리 툴바