0

I have seen some code in C/C++ that goes like this:

void Main(void)

And

int Main(void)

What is the reasoning for this: why is it not a parameter but used in parentheses after the void/int e.t.c name?

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
Joe
  • 528
  • 5
  • 19
  • It goes back to C. This question is a repeat of http://stackoverflow.com/questions/9545208/using-void-in-functions-without-parameter – Gavin Haynes Nov 16 '13 at 07:56
  • 2
    This is one of those questions where the answer depends on whether you mean C or C++. There is no such thing as a C/C++ language. – juanchopanza Nov 16 '13 at 07:58
  • I have rolled-back to keep the name capitalized `Main` because it `void main(void)` is an error. – Potatoswatter Nov 16 '13 at 08:13

6 Answers6

7

void in a parameter list is effectively a keyword, not specifying the type of anything. The reason for its existence is purely historical.

Early C did not have function prototypes or any syntax for parameter lists. A function definition would look like this:

foo()
int x; // These are the parameters.
float y;
{
    return x + (int) y;
}

There was "no need" for prototypes because the compiler would assume every call passed exactly the correct type and number of arguments. This was error-prone. Furthermore the return type was assumed to be int, which is an even more brittle assumption. The first prototypes specified only the return type.

float foo(); // Still no parameters.

When the familiar float foo( int x, float y ) syntax was introduced, backward compatibility with the preceding style was retained. The void keyword was used to differentiate between an indeterminate (missing) parameter list and an empty parameter list.

In C, float foo() and float foo( void ) mean different things to this day.

In C++, indeterminate parameter lists are unsupported, and float foo( void ) is a discouraged, but not deprecated, synonym for float foo().

Furthermore C++ allows templates to determine the type of an argument, but generating a (void) parameter list by a template is an error.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
3

In C (void) means that there is no arguments required in function call, while () means unspecified number of arguments. In C++. () means the same as (void)

Here is what C standard says

5.1.2.1 Freestanding environment

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. / .. / The effect of program termination in a freestanding environment is implementation-defined.

5.1.2.2 Hosted environment

A hosted environment need not be provided, but shall conform to the following specifications if present.

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char* argv[]) { /* ... */ }
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • -1: the given quote does not support the preceding answer, or seem to relate to the question. – Potatoswatter Nov 16 '13 at 08:14
  • @Potatoswatter you are correct, but actually it is confusing because OP used function name `Main` – Grijesh Chauhan Nov 16 '13 at 08:19
  • @GrijeshChauhan I'm a rather woozy right now for various reasons, and the question looked clear the first time I read it, but now it seems very poorly phrased. Anyway he seems to be asking about the `void` part and not the `main` part. – Potatoswatter Nov 16 '13 at 08:20
  • @GrijeshChauhan Quite strangely the other answer linked from the question comments claims the syntax is "a hangover." Go figure. – Potatoswatter Nov 16 '13 at 08:23
2

In C this

void func() {
}

means a function that takes an indeterminate number of arguments. If you want to write a function that takes zero arguments you do this

void func(void) {
}

The reasons for this are in the history of the C language, they don't make much sense otherwise. For this reason C++ changed the rules. In C++ the first example above means a function that takes zero arguments, and this

void func(int x, ...) {
}

means a function that takes a variable number of arguments (one or more in this case). C will also accept this.

So in C and C++, example 2 means zero arguments and example 3 means a variable number of arguments, but C and C++ disagree on what example 1 means, for C it's an indeterminate number of arguments, for C++ it's zero arguments.

If you see (void) in a C++ program it's either code that has been ported from C and no-one has bothered to change it, or it's code which is expected to be compiled as both C and C++ (in a header file for instance).

john
  • 85,011
  • 4
  • 57
  • 81
  • @Potatoswatter, that's what I said. – john Nov 16 '13 at 08:03
  • @Potatoswatter OK I'll update my answer, but I think the distinction between indeterminate and variable is hardly worth making. – john Nov 16 '13 at 08:04
  • OK changed, not completely certain what distinction you are making but I guess you're right. – john Nov 16 '13 at 08:12
  • your answer is partly wrong. Yoru first example, because it is a function *definition* still declares a function that receives no arguments. It is only in a pure declaration (without definition) where the distinction of a declaration with and without prototype makes a real difference. – Jens Gustedt Nov 16 '13 at 09:04
1

Draft n1570;

5.1.2.2.1 Program startup:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv.....)

It is clear from the C standard that void is used for a function having no parameter.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

In c++, sometimes parameter "names" may be excluded if they are not called from within the function. If you know that you will use it, you may add the parameter name by yourself. It would hardly make sense for the void parameter of main, though.

user1186155
  • 238
  • 2
  • 10
0

In old compilers it was mandate to specify void in parenthesis in case main is not going to accept any parameters. but these days you may need to use like only:

void main() 
ayahya82
  • 374
  • 3
  • 6
  • 18
  • -1, neither C nor C++ ever changed its respective meaning of `()`, and neither language ever allows `main` to return `void`. – Potatoswatter Nov 16 '13 at 11:47