Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is main with parameter list of void different from main with an empty parameter list? [duplicate]

Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?

What is a void ? Anyone provide some examples, proper use of void ? And what is the difference when we write void main (void) or main() ?

like image 776
Joseph Lee Avatar asked Dec 14 '12 10:12

Joseph Lee


People also ask

What is the difference between empty parameter list and function definition?

In short, an empty parameter list in a function declaration indicates that the function takes an unspecified number of parameters, while an empty parameter list in a function definition indicates that the function takes no parameters.

What is the difference between void and blank parameter list?

In C, a function with the parameter list (void) explicitly takes nothing for its arguments. That means the compiler can actually tell you you've made a mistake if you try to pass something. In C++, these function declarations are equivalent. A blank parameter list means "no parameters" the same as void does.

What is the difference between void main() and int main()?

The void main () indicates that the main () function will not return any value, but the int main () indicates that the main () can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main ().

Why can't we take parameters from a void type?

The void type tells the compiler that there is no 'entity' (no 'storage'), so takes an int but returns nothing. In the case of parameters this: which indicates more explicitly that it does not take parameters. Different story is with the type void * which is a type, a pointer to something dimensionless.


3 Answers

In C, in general, (void) means no arguments required in function call, while () means unspecified number of arguments.

e.g.

void foo(void)
{
   // body
}

void bar()
{
    //body
}

In calling enviroment,

foo();  // Correct 
foo(1); // Incorrect
bar();  // Correct
bar(1); // Also correct

This was the general explanation.

But for your case for main() , C99 Standard says that,

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[]) { /* ... */ } or equivalent;

or

in some other implementation-defined manner.

So, in this void main(void) return type should be int.

And at last , for main(), return type is not given so implicitly return type would be int.

like image 169
Omkant Avatar answered Oct 07 '22 05:10

Omkant


Excluding the return type of the main as in

main(){
}

doesn't mean that it's a void type, it depends on the compiler. I think it can be said it's generically interpreted as

int main(){
}

The void type tells the compiler that there is no 'entity' (no 'storage'), so

void func(int i)

takes an int but returns nothing. In the case of parameters this:

void func()

is equivalent to this:

void func(void)

which indicates more explicitly that it does not take parameters. Different story is with the type void * which is a type, a pointer to something dimensionless.

like image 26
lunadir Avatar answered Oct 07 '22 05:10

lunadir


Basically, void is a data type, which basically used with method declaration. It means nothing or no type. Eg:

1) int myFunc(void) -- the function takes nothing.

2) void myFunc(int) -- the function returns nothing

3) void* data; -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

like image 36
Ravi Avatar answered Oct 07 '22 06:10

Ravi