This question already has an answer here:
I am learning now c, and till now I have come up with many small programs. However i have noticed that many of them in the main function start like:
main()
{
//code
}
or
int main()
{
//code
return 0;
}
or
int main(void)
{
//code
return 0;
}
So, what should i use? or which the best way to follow? thanx!
Your first example uses a feature inherited from the outdated dialect of C which predated the first ANSI(1989) and ISO(1990) standard: namely, that you can write a function which doesn't specify its return type, and in that case the type defaults to
int
.In early C, the
void
keyword and associated type did not exist. When programmers wanted to write procedures ("functions that have a side effect, but do not return anything"), they simulated it using this feature. They wrote a function without any keyword specifying the return type. They allowed the function to execute to it last statement without returning a value (or alternatively, they usedreturn;
to exit from the middle without supplying a value), and they wrote the calls to the function such that those calls did not try to use the return value:Unfortunately, some programmers also started not caring about the termination status of a program and writing
main
itself in this procedure style:(A mixed style also existed: omitting the
int
declarator but returning an integer value.)These programs failing to return a value had an indeterminate termination status. To the operating system, their execution could look successful or failed. Woe to the script writer who tried to depend on the termination status of such a program!
Then things took a turn for the worse. C++ came along and introduced
void
, and it was adopted into C. With thevoid
keyword in C++, one could declare a function that actually returns nothing (and make it an error to have areturn;
statement in any other kind of function). The dummy programmers who used to writemain
with no return type got dumber, and started sticking this new-fangled, fresh-out-of-C++void
in front:By this time they had forgotten that when they wrote
main()
, it actually meantint main()
, which made the function have a compatible type with the startup call invoked by the environment (except for the matter of neglecting to return a value). Now they actually had a different function type from the expected one, which might not even be successfully called!Where things stand now is that in C++ and in the latest C++ standard,
main
is still required to return anint
. But both languages make a concession for the original dummy programmers: you can let execution "fall off" the end ofmain
and the behavior is as ifreturn 0;
had been executed there. So this trivial program now has a successful termination status as of C99 and, I think, C++98 (or possibly earlier):But neither language makes a concession for the second-generation dumber programmers (and everyone else who read the C books that those programmers wrote in the 1980's and since). That is,
void
is not a valid return declarator formain
(except where it is documented by platforms as being accepted, and that applies to those platforms only, not to the portable language).Oh, and allowance for the missing declarator was removed from C in C99, so
main() { }
is no longer correct in new dialects of C, and isn't valid C++. Incidentally, C++ does have such a syntax elsewhere: namely, class constructors and destructors are required not to have a return type specifier.Okay, now about
()
versus(void)
. Recall that C++ introducedvoid
. Furthermore, though C++ introducedvoid
, it did not introduce the(void)
argument syntax. C++ being more rigidly typed introduced prototype declarations, and banished the concept of an unprototyped function. C++ changed the meaning of the()
C syntax to give it the power to declare. In C++,int func();
declares a function with no arguments, whereas in C,int func();
doesn't do such a thing: it declares a function about which we do not know the argument information. When C adoptedvoid
, the committee had an ugly idea: why don't we use the syntax(void)
to declare a function with no arguments and then the()
syntax can stay backward compatible with the loosey-goosey legacy behavior pandering to typeless programming.You can guess what happened next: the C++ people looked at this
(void)
hack, threw up their arms and copied it into C++ for the sake of cross-language compatibility. Which in hindsight is amazing when you look at how the languages have diverged today and basically no longer care about compatibility to that extent. So(void)
unambiguosly means "declare as having no arguments", in both C and C++. But using it in C++ code that is obviously pure C++ never intended to be C is ugly, and poor style: for instance, on class member functions! It doesn't make much sense to write things likeclass Foo { public: Foo(void); virtual ~Foo(void) /*...*/ };
Of course, when you define a function like
int main() { ... }
, the function which is defined has no arguments, regardless of which language it is in. The difference is in what declaration info is introduced into the scope. In C we can have the absurd situation that a function can be fully defined, and yet not declared, in the same unit of program text!When we write
main
, usually it is not called from within the program, and so it doesn't matter what the definition declares. (In C++,main
must not be called from the program; in C it can be). So it is immaterial whether you writeint main()
orint main(void)
, regardless of whether you're using C or C++. The thing which callsmain
does not see any declaration of it (that you write in your program, anyway).So just keep in mind that if you write:
then although it is perfect C++ and correct C, as C it has a slight stylistic blemish: you're writing an old-style pre-ANSI-C function that doesn't serve as a prototype. Though it doesn't functionally matter in the case of
main
, you may get a warning if you use some compilers in a certain way. For instance, GCC, with the-Wstrict-prototypes
option:Because
-Wstrict-prototypes
is a darn useful warning to turn on when programming in C, for improved type safety, (along with-Wmissing-prototypes
), and we strive to eliminate warnings from our compile jobs, it behooves us to write:which will make that diagnostic go away.
If you want
main
to accept arguments, then it isint main(int argc, char **argv)
where the parameter names are up to you.In C++, you can omit parameter names, so this definition is possible, which serves nicely in the place of
main()
.Since the argument vector is null-pointer-terminated, you don't need
argc
, and C++ lets us express that without introducing an unused variable:The above line give you an error. The default return type of any function in c is int. As the above code return nothing it gives you an error.
In above code it fulfill all requirement so the above code will run.In above code we pass no argument in the function. So this function can take global and local variables to process.
In above code we pass no argument in the function. But specifying
void
tells the compiler that it does not take any argument.void
is the default datatype of argument that signifies no input.By default main function returns an integer type, hence its "int main()" or you can give simply "main()"
"main(void)" is same as "main()", it tells the compiler that main function has no arguments.
In case if you want to pass arguments via main function:
int main(int argc, char *argv[]){ return 0; }
first :
declares a function main - with no input parameters. Although main should have returns ( your compiler will take care of this )
2nd/3rd:
Declare a function main which returns an int and takes in no input parameters
You should use 3rd format. Rather this is the best way:
You should use 1 one of these 4 choices:
where it's conventional to use the names
argc
andargv
; you can change them but don't.Take care never to use
void main(void);
which is too-often seen in production code.For Standard C
This (is valid in C89)
main()
implicitly meant (previously)int main(void)
. However the default return type rule has been abandoned in C99. Also:main()
means - a functionmain
taking an unspecified number of arguments of.main(void)
means "a functionmain
taking no arguments.