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()
?
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()
?
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
.
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.
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
Void means "emptyness". In your example of void main() it means that the functions main() does not return a value. I feel obliged tell you that void main() should be avoided (no pun intended) at all costs, use int main() instead. int main() makes sure your program can return a value of type int to the OS on close. There are numerous other uses of void, check out this website if you want to read more about this.
void
is a data type with no values. It is also an incomplete data type that cannot be completed. When used as a return type for a function, it indicates that the function does not return a value:
void foo(int x);
When used as a parameter list, it indicates that the function takes no arguments:
void bar(void);
This is different from an empty parameter list, which indicates that the function takes an unspecified number of arguments (in C; in C++, an empty parameter list is the same as using void
):
void bletch();
No object (variable) can be typed void
. However, you can declare pointers of type void *
: these are "generic" pointers, and can be converted to and from other pointer types without an explicit cast. The standard memory allocation functions malloc
, calloc
, and realloc
all return void *
:
double *darr = malloc(sizeof *darr * rows);
In a hosted implementation (basically, anything with an operating system), main
must be declared as
int main(void)
or
int main(int argc, char **argv) // the parameter names can be whatever you want,
// but argc and argv are the usual convention;
// char **argv is equivalent to char *argv[]
or in some other implementation-defined manner; an implementation may accept
void main()
as a legitimate signature for main
, but it must explicitly document that somewhere.