19

When a C function does not accept any arguments, does it have to be declared/defined with a "void" parameter by the language rules? PC-Lint seems to have problems when there's nothing at all in the argument-list, and I was wondering if it's something in the language syntax that I don't know about.

Edit: I just found a duplicate (back-dupe? it came first) question, C void arguments, which has more answers and explanations.

cafce25
  • 15,907
  • 4
  • 25
  • 31
noamtm
  • 12,435
  • 15
  • 71
  • 107

2 Answers2

37

void means the function does not take any parameters. For example,

int init (void)
{
    return 1;
}

This is not the same as defining

int init ()
{
    return 1;
}

because in the second case the compiler will not check whether the function is really called with no arguments at all; instead, a function call with arbitrary number of arguments will be accepted without any warnings (this is implemented only for the compatibility with the old-style function definition syntax, pre-ANSI).

dfa
  • 114,442
  • 31
  • 189
  • 228
7

IIRC func(void) in C will declare a function that takes no parameters whereas func() declares a function that will take any number of parameters. I believe the latter is an artifact coming from pre-ANSI C.

According to Wikipedia here, the declaration func() does basically declare the function "without information about the parameters".

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
  • Isn't a function which takes any number of parameters defined as func(...) ? – noamtm Jul 22 '09 at 08:44
  • 2
    You believe correct, in K&R C parameter lists were defined different (and poorly). But in C++ f() is the same as f(void) – H H Jul 22 '09 at 08:49
  • 2
    @noamtm - a function of the form func(a, b, ...); declares a C function that takes a variable argument list so in a sense that would also take any number of parameters. – Timo Geusch Jul 22 '09 at 08:55