Generally we don't concern anything above main() or main(void). both look similar to us but there is a difference between both and it is useful too.
Consider the following two definitions of main().
and
What is the difference?
In C++, there is no difference, both are same.
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run following two C programs (remember to save your files as .c). Note the difference between two signatures of fun().
The above program compiles and runs fine , but the following program fails in compilation
C language |
Consider the following two definitions of main().
int main() { /* */ return 0; } |
int main( void ) { /* */ return 0; } |
In C++, there is no difference, both are same.
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run following two C programs (remember to save your files as .c). Note the difference between two signatures of fun().
// Program 1 (Compiles and runs fine in C, but not in C++) void fun() { } int main( void ) { fun(10, "GfG" , "GQ" ); return 0; } |
The above program compiles and runs fine , but the following program fails in compilation
// Program 2 (Fails in compilation in both C and C++)
void
fun(
void
) { }
int
main(
void
)
{
fun(10,
"GfG"
,
"GQ"
);
return
0;
}
Unlike C, in C++, both of the above programs fails in compilation. In C++, both fun() and fun(void) are same.
This is important to know about scanf()- SCANF() - Some important features.
0 comments:
Post a Comment