-1

Having done most of my dev deeds in C++ and C#, I find the habit in C of specifying an empty parameter list with "void" a bit peculiar, but seeing it in "trustworthy sources" makes me assume it's required-ish. The question is why?

To clarify, in for instance Visual Studio, I cannot with any combination of enabled C compiler switches, and disabled C++ compiler switches, get even as much as a compiler warning when writing this:

void foo_bar();

Then, what is it that makes it necessary-ish to instead declare it with this:

void foo_bar(void);

Is it just old C standardese that is lingering that is not needed with later C standards?

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
  • @Emilien, @ RaymondChen it's not quite a dupe of either because it doesn't ask the same question. It doesn't ask "what's the difference" it asks why there is no error when using one vs the other. – Mihai Stancu Jun 12 '14 at 08:02

1 Answers1

2

Backstory:

When declaring functions in C (any symbol actually) you need declare everything in the proper order (declare a function before using it) otherwise you'll get errors regarding undefined symbols.

Forward declaration of functions allows you to accomplish this by declaring only the function signature (without the body). This allows the compiler to identify function names (symbols) and later use them before they are completely defined.

When using forward declarations to export the publicly accessible functions of a library (in your public header files) you will declare the entire signature so that users of your library will know what parameters are expected.

When using forward declarations for your own internal use you (sometimes) won't need to declare the entire signature of the function, only the return type and name will suffice -- no parameters. Only when you finally define the function will you specify the parameter list.

Direct answer:

Yes it's just old C standardese and nothing more. Because the same C standardese says that empty brackets means an unknown number of parameters (not necessarily 0).

Related reading:

This question I've asked some time ago tries to fathom (like you) the utility of declaring functions with "an unknown number of parameters" -- apparently in a very misconstrued way.

Community
  • 1
  • 1
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • 2
    Empty parentheses doesn't mean "any number of parameters." It means "This function takes a fixed number of parameters, but I don't know how many." The difference is subtle but significant. – Raymond Chen Jun 12 '14 at 08:02
  • @RaymondChen yes, you're right, i'll update. – Mihai Stancu Jun 12 '14 at 08:03