English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can anyone give the explanation for the following
main()
int main()
void main()
main(void)
void main(void)
int main(void)

2006-09-09 18:44:17 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

main() -- forbidden
int main() -- standard way to declare the main entrance of the program. return integer: 0 for normal exit; non-0 for error exit.
void main() -- forbidden (must return a value)
main(void) -- same as main()
void main(void) -- same as void main()
int main(void) -- same as int main(void)

2006-09-09 18:48:34 · answer #1 · answered by churinga 2 · 0 1

main() = NO!

int main() = works in C under some compilers, legal for C++ or C99 compilers.

void main() = works under either C or C++, not a good idea !

main(void) = NO!

void main(void) = works everywhere, not good practice!

int main(void) = works everywhere all the time and well. Use it.

Okay, for a bit more explanation, let's talk about what we need to know about main. The main function is a special case among C and / or C++ functions. It is tightly specified as to what is required because the main function is the entry point code and exit point of the entire program. Therefore, it must interface correctly with the operating systems being used. As in every C or C++ function, the return type is specified first. There are, much to the chagrin of us all, three valid ANSI standards for behavior related to the return type of a function. One for C as was originally used, one for C++ and one called C99 which is an attempted update of the C language standards to be more compatible with C++. We are now on the verge of understanding part of this mess, stay with me. The original C standard *REQUIRES* all return types to be specified and a matching return statement included. Both C99 and C++ make the statement that in lieu of a specified return , each function will return an integer with value of zero automatically. They don't support functions without return types. Void is considered a valid return type in functions by all three. In main functions, however; the return value is passed to the OS as an indication of termination errors. That makes it *REALLY* important in some few cases and irrelevent in a lot more. There is no real penalty for making the effort to use return types and statements in all functions correctly, so why not get in the habit and let it come naturally?
Now, consider what each says about parameter lists. The original ANSI C says that the type void is used if the function doesn't take a parameter. C99 and C++ say that if they don't find a type inside the parentheses, then the type is void or null automatically. Again, other than typing four whole letters each time, I'd say use the void and keep it clear for all your code. Empty parentheses may be more efficient for typists, but lazy never won an award for accurate or readable. Just my opinion, but the original way guards against cross platform or cross compiler problems, makes the program more readable in the sense that it leaves nothing to guess at and consistency always wins in programming.

2006-09-12 12:37:43 · answer #2 · answered by griz803 5 · 0 0

() and "void" mean the same thing.
"void" before a function declaration means no return value
int before a function declaration means return an integer

main is the entry for any c program. There are two typical ways to declare a main, with and without parameters:

int main (void)
int main (int argc, char *argv())

where argc is the number of arguments and argv is an array of strings.

For example if I ran the program like such:
myprogram.exe one two three

then argc would be 4 and argv would be
argv[0] = "myprogram.exe"
argv[1] = "one"
argv[2] = "two"
argv[3] = "three"

2006-09-11 01:20:08 · answer #3 · answered by soulblazer28 2 · 0 0

main()==>It is same as "int main()",if u want to return some value of integer type from main function use this,by default it indicate int. If u use this type of main declaration then u have to return 0 otherwise compiler gives u error.
int main()==>same as "main()", same description.
void main()==>It indicates main function does not return any value ,void means empty i.e. u just process in main function and get out put in that function or using other sub functions,but it can't return any value.
main(void)==>Its' return type is Integer but arguments are void (empty) same as "int main()"
void main(void)==>It is same as "void main()"
int main(void) ==>Same as "main()" or "main(void)"

If u satisfied with my ans. than OK. otherwise use forllowing authors ,their books are rally help in also another concept of C
>>Balagurusamy
>>Yashavant kanedkar

2006-09-10 02:13:40 · answer #4 · answered by Swati 1 · 0 0

main() this is a function from which the program starts to operate
void main() means you wouldnt return anything
int main() is a function which would return an integer

2006-09-10 02:00:17 · answer #5 · answered by george grohan mendal 3 · 0 0

fedest.com, questions and answers