Different conventions for main() in C [duplicate] - c

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 5 years ago.
My only exposure to programming has been Java,where I have not encountered (up to now) different conventions for writing the main method.I have been following to sources for learning c (K&R AND C Programming A Modern Approach) where they use very different forms of the main method (function).
K&R version up to now:
main() {
blah blah blah;
}
C Programming A Modern Approach
int main() {
blah blah blah;
return 0;
}
OR
int main() {
blah blah blah;
//returns nothing
}
To make matters more confusing I have seen people do :
int main(void) {
blah blah blah;
}
while they either returned 0 or did not.
I don't in my very uneducated assumption think this is only a standards issue but maybe something a bit more conceptual or deep.
Could someone shed some light on this issue?

K&R style is outdated and isn't correct according to the C standard any more.
Valid signatures are
int main(void)
and
int main(int argc, char *argv[])
or, equivalent because an array type in a function is adjusted to a pointer type anyways:
int main(int argc, char **argv)
The signature
int main()
happens to be valid as well, because an empty argument list means any number of arguments, that aren't described *). AFAIK, this is subject to change, so don't write it this way. Writing void is how you express this function doesn't take arguments in C.
Implementations of C are free to provide other implementation-defined entry points. But the two I listed above are the only ones guaranteed by the standard.
C99 introduced a special rule for main() that states if the function doesn't return anything, a value of 0 is returned implicitly. So only in main, you can skip the return. My advice is: don't. It's just confusing. But this is an opinion.
*) note this is different in C++, where nothing between the parantheses indeed means: no arguments.

Pre-standardized versions of C (known as K&R C) had the concept of a default type of int if none was given. So in K&R this:
main() {
Is the same as:
int main() {
As for the difference between int main() and int main(void), an empty parameter list means the function takes an unspecified number of parameters while (void) as a parameter list means the function takes no parameters. The former is acceptable, but the latter is preferred as it is more explicit.
Regarding the use of the return statement, a function with a non-void return type must use return to return a value, except (starting with the C99 standard) for the main function. In the case of main, a missing return statement implies a return value of 0.
Because the implicit return 0 for main was added in C99, you'll see some code that explicitly returns and some that doesn't depending on which version of the standard the programmer is conforming to.

There are two standard signatures for main as of the latest C language standard:
int main( void ) // void indicates "takes no arguments"
and
int main( int argc, char *argv[] ) // or char **argv, it means the same thing in this context
The names argc and argv are arbitrary; you can use different names if you wish.
Implementations may provide additional valid signatures for main - check your compiler documentation.
If your program does not take any command line arguments, use the first form, otherwise use the second.
C function declaration and definition syntax has evolved a bit over time, which is why different references use different conventions.
Implicit Typing
First of all, C originally allowed for implicit int declarations - if the compiler saw a function definition or declaration without a type specifier, it assumed the function returned int:
foo(); // modern equivalent: int foo( void )
This was allowed up until the 1999 standard, although was considered bad style by most of us long before then.
The void keyword was not introduced until the 1989 standard. As a type specifier, it indicates a type with no values. As an identifier in a function parameter list, it indicates that the function takes no arguments.
Before the introduction of the void keyword, there was no good way (other than documentation) to distinguish between functions that returned a value you were meant to use vs. functions that just executed some action. Some of us used the "implicit int" convention to indicate those functions weren't meant to return anything meaningful:
foo(); /* returns int, but return value not meant to be used */
int bar(); /* returns int, return value meant to be used */
Again, this was just a convention, and far from universal. Now we'd use the void keyword:
void foo(); /* does not return a value */
int bar(); /* returns int */
Function Prototype Syntax
Originally, a function definition looked something like this:
foo( bar, bletch, blurga )
int bar;
double bletch;
char *blurga;
{
/* function body */
}
The parameter list only specified the names of the parameters, not their types; that was done in a separate set of declarations between the function declarator and the opening { of the function body. A function that took no arguments had an empty identifier list in the function definition:
blah( )
{
/* function body */
}
Function declarations only specified the name and return type of the function; it didn't specify the parameters at all:
foo( ); /* no bar, bletch, or blurga */
An empty identifier list in a function declaration indicates that the function takes an unspecified number of arguments, not zero arguments. The compiler could check that the return type of the function call was being used correctly, but it could not check that the number and types of parameters in the call were correct.
The 1989 language standard introduced the concept of function prototype syntax, where the type of each parameter was specified along with its name in the parameter list:
foo( int bar, double bletch, char *blurga )
{
/* function body */
}
This applied to both the function declaration and definition:
foo( int bar, double bletch, char *blurga );
This change allowed the compiler to check that the number and types of parameters in a function call were correct, along with the return type. In addition, the void keyword could be used in the parameter list to indicate that the function took no parameters, in both the definition and declaration:
blah( void ); /* declaration, returns int, takes no parameters */
blah( void ) /* definition */
{
/* function body */
}
So yeah, depending on which language revision you're working against, main would be written differently:
K&R:
main() main( argc, argv )
{ int argc;
... char *argv[];
} {
...
}
C89:
main( void ) main( int argc, char *argv[] )
{ {
... ...
} }
C99 and later:
int main( void ) int main( int argc, char *argv[] )
{ {
... ...
} }

C standard defines signature for main either as
int main(void)
or
int main(int argc, char *argv[])
Adding return 0; as a last statement in main function is optional.
Standard also says about some implementation defined prototype. int main() is accepted by GCC compiler.
main() is an old school prototype and almost deprecated.

Related

"Return type of main is not int" warning on macOS [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 5 years ago.
So I have a MacBook Pro 2017 and whenever I compile a program that as "void main", the compiler gives me a warning saying that the return type of main is not int...
void main(){
(...)
}
Like the warning says, you should define the return type as an int:
int main() {
// bunch of code...
return 0;
}
Current versions of the C standard require that the main function has a return type of int. So you need to change the definition to int main() and have it return a value.
Section 5.1.2.2.1 of the C standard detailing hosted environments states the following:
1 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.
Note that a definition of int main() is allowable as the empty parameter list means the function takes no parameters. From section 6.7.6.3:
14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator
that is part of a definition of that function specifies that the
function has no parameters. The empty list in a function declarator
that is not part of a definition of that function specifies that no
information about the number or types of the parameters is supplied.
Specifying the main function as void main() is a pre-standardized variant from the K&R days and is no longer valid.
It's expected that main() will return an int value. A minimal start is:
int main() {
return 0;
}
A more formal version which accepts command-line parameters:
int main(int argc, char** argv) {
return 0;
}
Declaring it with a different type makes it incompatible. Some older compilers may not care, but Xcode's does.
C has been around a long time and has evolved considerably from its early days. If you're using an older reference you may find examples with really strange notation, or conventions that are no longer value.

Is int main() { } (without "void") valid and portable in ISO C?

The C standard specifies two forms of definition for main for a
hosted implementation:
int main(void) { /* ... */ }
and
int main(int argc, char *argv[]) { /* ... */ }
It may be defined in ways that are "equivalent" to the above (for
example, you can change the parameter names, replace int by a typedef
name defined as int, or write char *argv[] as char **argv).
It may also be defined "in some other implementation-defined manner"
-- which means that things like int main(int argc, char *argv[],
char *envp) are valid if the implementation documents them.
The "in some other implementation-defined manner" clause was not in
the 1989/1990 standard; it was added by the 1999 standard (but the
earlier standard permitted extensions, so an implementation could
still permit other forms).
My question is this: Given the current (2011) ISO C standard, is a
definition of the form
int main() { /* ... */ }
valid and portable for all hosted implementations?
(Note that I am not addressing either void main or the use of
int main() without parentheses in C++. This is just about the
distinction between int main(void) and int main() in ISO C.)
No.
According to the normative wording of the standard, a definition
using empty parentheses without the void keyword is not one of the
forms that must be accepted, and strictly speaking the behavior of
such a program is undefined.
Reference:
N1570
section 5.1.2.2.1. (The published 2011 ISO C standard, which is not
freely available, has the same wording as the N1570 draft.)
Paragraph 1 says:
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.
The use of the word "shall" outside a constraint means that any
program that violates it has undefined behavior. So if, for example, I write:
double main(unsigned long ocelots) { return ocelots / 3.14159; }
a conforming compiler isn't required to print a diagnostic, but it's
also not required either to compile the program or, if it does compile
it, to have it behave in any particular manner.
If int main() were equivalent to int main(void), then it
would be valid and portable to any conforming hosted implementation.
But it's not equivalent.
int main(void) { }
provides both a declaration (in this case, a prototype) and a
definition. The declaration, by using the void keyword, specifies that the function has no parameters. The definition specifies the same thing.
If I instead write:
int main() { }
then I'm using an old-style declaration and definition. (Such
declarations and definitions are obsolescent, but they're still
part of the language definition, and all conforming compilers must
still support them.)
As a declaration, it doesn't specify the number or type(s) of arguments
expected by the function. As a definition, it defines no parameters,
but compilers needn't use that information to diagnose incorrect calls.
DR #317 includes the C standard committee's 2006 ruling that a definition with () does not provide a prototype equivalent to one with (void) (thanks to hvd for finding that reference).
C allows main to be called recursively. Suppose I write:
int main(void) {
if (0) {
main(42);
}
}
The visible prototype int main(void) specifies that main takes
no arguments. A call that attempts to pass one or more arguments
violates a constraint, requiring a compile-time diagnostic.
Or suppose I write:
int main() {
if (0) {
main(42);
}
}
If the call main(42) were executed, it would have undefined behavior
-- but it doesn't violate a constraint, and no diagnostic is required.
Since it's protected by if (0), the call never happens, and
the undefined behavior never actually occurs. If we assume that
int main() is valid, then this program must be accepted by any
conforming compiler. But because of that, it demonstrates that
int main() is not equivalent to int main(void), and therefore
is not covered by 5.1.2.2.1.
Conclusion: Following the wording of the standard, an
implementation is permitted to document that int main() { } is
permitted. If it doesn't document it, it's still permitted to accept
it without complaint. But a conforming compiler may also reject
int main() { }, because it is not one of the forms permitted by
the standard, and its behavior is therefore undefined.
But there's still an open question: Was that the intent of the authors
of the standard?
Prior to the publication of the 1989 ANSI C standard, the void
keyword did not exist. Pre-ANSI (K&R) C programs would define main
either as
main()
or as
int main()
A major goal of the ANSI standard was to add new features (including
prototypes) without breaking existing pre-ANSI code. Stating that
int main() is no longer valid would have violated that goal.
My suspicion is that the authors of the C standard did not intend
to make int main() invalid. But the standard as written does not
reflect that intent; it at least permits a conforming C compiler
to reject int main().
Practically speaking, you can almost certainly get away with it.
Every C compiler I've ever tried will accept
int main() { return 0; }
without complaint, with behavior equivalent to
int main(void) { return 0; }
But for a variety of reasons:
Following both the letter and the intent of the standard;
Avoiding the use of an obsolescent feature (a future standard could remove old-style function definitions);
Maintaining good coding habits (the difference between () and (void) is important for functions other than main that are actually called by other functions).
I recommend always writing int main(void) rather than int main().
It states the intent more clearly, and you can be 100% sure that your
compiler will accept it, rather than 99.9%.
A strong indication that int main() is meant to be valid, regardless of whether the standard accurately gives the wording to make it valid, is the fact that int main() is occasionally used in the standard without anyone raising any objection. While examples are not normative, they do indicate intent.
6.5.3.4 The sizeof and _Alignof operators
8 EXAMPLE 3 In this example, the size of a variable length array is computed and returned from a function:
#include <stddef.h>
size_t fsize3(int n)
{
char b[n+3]; // variable length array
return sizeof b; // execution time sizeof
}
int main()
{
size_t size;
size = fsize3(10); // fsize3 returns 13
return 0;
}
6.7.6.3 Function declarators (including prototypes)
20 EXAMPLE 4 The following prototype has a variably modified parameter.
void addscalar(int n, int m,
double a[n][n*m+300], double x);
int main()
{
double b[4][308];
addscalar(4, 2, b, 2.17);
return 0;
}
void addscalar(int n, int m,
double a[n][n*m+300], double x)
{
for (int i = 0; i < n; i++)
for (int j = 0, k = n*m+300; j < k; j++)
// a is a pointer to a VLA with n*m+300 elements
a[i][j] += x;
}
As for the actual normative text of the standard, I think too much is being read into "equivalent". It should be pretty clear that
int main (int argc, char *argv[]) {
(void) argc; (void) argv;
return 0;
}
is valid, and that
int main (int x, char *y[]) {
(void) argc; (void) argv;
return 0;
}
is invalid. Nonetheless, the standard explicitly states in the normative text that any names may be used, meaning that int main (int argc, char *argv[]) and int main (int x, char *y[]) count as equivalent for the purposes of 5.1.2.2.1. The strict English meaning of the word "equivalent" is not how it is meant to be read.
A somewhat looser interpretation of the word is what Keith Thompson suggests in his answer.
An equally valid even looser interpretation of the word does allow int main(): both int main(void) and int main() define main as a function returning int and taking no parameters.
Neither the standard nor any official DRs currently answer the question of which interpretation is intended, so the question is unanswerable, but the examples strongly suggest that last interpretation.
Yes.
int main() { /* ... */ }
is equivalent to
int main(void) { /* ... */ }
N1570 5.1.2.2.1/1
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.
6.7.6.3/14
An identifier list declares only the identifiers of the parameters of the function. An empty
list in a function declarator that is part of a definition of that function specifies that the
function has no parameters. The empty list in a function declarator that is not part of a
definition of that function specifies that no information about the number or types of the
parameters is supplied.
(emphasis mine)
As is clearly stated by the standard, the definition int main() { /* ... */ } does specify that the funtion main has no parameters. And it is clear to all of us, that this function definition does specify that the return type of the function main is int. And, since 5.1.2.2.1 does not require the declaration of main to have a prototype, we can safely affirm that the definition int main() { /* ... */ } satisfies all the requirement imposed by the standard (It [the main funtion] shall be defined with a return type of int and with no parameters, or [some other forms] .).
Nonetheless you should never use int main() {} in your code, because "The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature." (6.11.6), and because this form of definition does not include a function prototype declarator, the compiler won't check whether the number and types of arguments are correct.
N1570 6.5.2.2/8
No other conversions are performed implicitly; in particular, the number and types of
arguments are not compared with those of the parameters in a function definition that
does not include a function prototype declarator.
(emphasis mine)

What are the differences between: main(){}, int main(){} and int main(void){} [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Why is the type of the main function in C and c++ left to the user to define? [duplicate]
(6 answers)
Closed 9 years ago.
I am currently learning C and I have written many small programs. However, I have noticed that the main function could start as
main()
{
//code
}
or
int main()
{
//code
return 0;
}
or
int main(void)
{
//code
return 0;
}
Which option should I use? Thanks!
For Standard C
For a hosted environment (that's the normal one), the C99 standard
says:
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;9) or in some other implementation-defined manner.
9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and
so on.
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 function main taking an unspecified number of arguments of.
main(void) means "a function main taking no arguments.
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 used return; 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:
parse_input() /* similar to a procedure in Pascal, but fake! */
{
/* ... */
if (condition())
return; /* no value */
/* ... */
/* fall off end here */
}
int main()
{
parse_input(); /* no return value extracted, everything cool! */
return 0;
}
Unfortunately, some programmers also started not caring about the termination status of a program and writing main itself in this procedure style:
main()
{
/* do something */
/* fall off the end without returning a value */
}
(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 the void keyword in C++, one could declare a function that actually returns nothing (and make it an error to have a return; statement in any other kind of function). The dummy programmers who used to write main with no return type got dumber, and started sticking this new-fangled, fresh-out-of-C++ void in front:
void main() /* yikes! */
{
/* do something */
/* fall off the end without returning a value */
}
By this time they had forgotten that when they wrote main(), it actually meant int 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 an int. But both languages make a concession for the original dummy programmers: you can let execution "fall off" the end of main and the behavior is as if return 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):
int main()
{
}
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 for main (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++ introduced void. Furthermore, though C++ introduced void, 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 adopted void, 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 like class 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 write int main() or int main(void), regardless of whether you're using C or C++. The thing which calls main does not see any declaration of it (that you write in your program, anyway).
So just keep in mind that if you write:
int main() /* rather than main(void) */
{
}
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:
test.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
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:
int main(void) /* modern C definition which prototypes the function */
{
}
which will make that diagnostic go away.
If you want main to accept arguments, then it is int 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().
int main(int, char **) // both arguments ignored: C++ only
{
}
Since the argument vector is null-pointer-terminated, you don't need argc, and C++ lets us express that without introducing an unused variable:
#include <cstdio>
int main(int, char **argv) // omitted param name: C++ only
{
// dump the arguments
while (*argv)
std::puts(*argv++);
}
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:
int main(int argc, char *argv[]){
return 0;
}
You should use 1 one of these 4 choices:
int main(void);
int main();
int main(int argc, char **argv);
int main(int argc, char *argv[]);
where it's conventional to use the names argc and argv; you can change them but don't.
Take care never to use void main(void); which is too-often seen in production code.
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;
}
main(){}
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.
int main(){
//body
return 0;
}
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.
int main(void)
{
//code
return 0;
}
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.

main() function defined without return type gives warning

This is my program:
main()
{
printf("hello world\n");
}
I get this warning when compiling it:
function should return a value
When changing main() to void main(), the warning disappears.
Why is that so?
There are few things which you should take note of :
The int is the main() function's return type. That means that the kind of value main() can
return is an integer.
main( ) was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.
void main() is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore,
compilers don't have to accept this form, and several don't. Again, stick to the standard form,
and you won't run into problems if you move a program from one compiler to another.
And one last thing , instead of writing main like this :
int main() // here you are being silent about passing arguments to main , meaning it may or may not take arguments
write like this :
int main(void)// this specifies there are no arguments taken by main
You might wanna look at the C99 standard for further details.
Quick summary: If you don't want to use command-line arguments, you should write:
int main(void) {
/* body of main function */
}
If you do:
int main(int argc, char *argv[]) {
/* ... */
}
These are the only portable ways to define the main function.
You should probably have a return 0; at the end, though it's not strictly necessary. Returning 0 indicates successful execution. There are ways to indicate that execution failed; I won't get into that here.
There's some history behind this. The rules for a valid definition of the main function have changed a bit across different releases of the C standard.
Before the introduction of the first official standard for C in 1989, the most common form was:
main()
{
/* ... */
}
Or, if you wanted to use command-line arguments:
main(argc, argv)
/* argc is implicitly of type int */
char *argv[];
{
/* ... */
}
There was no way to define a function that didn't return a value. If you didn't specify a return type, it defaulted to int.
The 1989 ANSI C standard (which was republished with editorial changes as the 1990 ISO C standard) introduced prototypes, function declarations and definitions that specify the parameter types. There are two equally valid definitions for main. You can use one or the other depending on whether you need to use command line arguments:
int main(void) {
/* ... */
}
or
int main(int argc, char *argv[]) {
/* ... */
}
(char *argv[] can also be written as char **argv. This rule applies only to parameter definitions.)
A given compiler may or may not choose to permit other forms. For example, some compilers support a third parameter envp.
Somehow, some authors have gotten the idea that void main() or void main(void) is valid. It can be valid for some particular compiler, but only if that compiler explicitly supports it. It's not portable. The odd thing about this is that the same standard that first introduced the void keyword simultaneously established the rule that main's return type is int.
void main() is useful as an indicator that the author of the book you're reading doesn't know the C language very well, and that you should find another book.
The story is different for "freestanding" (embedded) systems. For such systems, the program's entry point is entirely implementation-defined, and might not even be called main. Defining it as void main(void) may well be valid for such systems.
The 1999 ISO C standard dropped the "implicit int" rule. Taking advantage of that rule was probably never a good idea in the first place. As of ISO C 1990, you could legally use:
main(void) { /* ... */ }
because it was equivalent to:
int main(void) { /* ... */ }
As of the 1999 standard, the int is mandatory.
The 1999 standard also added a special-case rule: reaching the closing } of the main function is equivalent to executing return 0;. It's still not a bad idea to add the explicit return 0;, especially if your code might be compiled with a pre-C99 compiler.
The 2011 ISO C standard didn't make any changes in this area.
The difference between int main() and int main(void) is that the latter explicitly says that main takes no arguments; the former doesn't specify how many arguments it takes. Use the int main(void) form. There have been debates about whether int main() is even legal.
You can likely get away with writing void main(), since it's an error that compilers are not actually required to diagnose (it's undefined behavior unless the implementation documents it).
The bottom line: The proper definition of main has a long and varied history, and there are a lot of variant forms you can probably get away with using. But unless you're programming for an embedded system, there is no point in using anything other than one of the two officially valid forms:
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
c automatically implies the datatype int to functions with no declared datatype. So as far as the compiler is concerned the above is:
int main()
{
printf("hello world\n");
}
This expects that you would return an integer at the end of it with a return statement. If you explicitly specify it as void main() you are telling the compiler that the function does not have a return value, hence no warning.
The reason that this is not an error is that if not specified, main() will return 0; at the end of execution. However the compiler is still giving you a warning that this is happening.
Best practice is to use int main() and then return 0 at the end of your program execution like this.
int main()
{
printf("hello world\n");
return 0;
}
See: this question for more information.
You got the warning because you didn't specify the return type of main.
You should always use int main, and return an int number, usually 0 for success.
int main()
{
printf("hello world\n");
return 0; //you can omit this since C99
}
Using void main on a hosted environment(normally we are, if not, the following doesn't have to be true) leads to undefined behavior, even though it works in some compilers, never use it.
The standard says main has two kinds of prototype, both returns int:
C11 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;10) or in some other implementation-defined manner.
write
return 0 ;
at the last line.

Difference between int main() and int main(void)?

What does the following mean :
int main(void) {...}
VS
int main() {...}
?
I think that int main() {...} means that main doesn't receive any parameters (from command line) , however:
int main(int argc, char *argv[])
does.
But, what does int main(void) {...} mean? And, what does void stand for ?
I've looked here but it's somehow a different question .
In C++, there is no difference.
In C, the difference is questionable. Some love to argue that the latter version (the one without void) is technically just a common implementation extension and not guaranteed to work by the standard because of the wording in the standard. However, the standard clearly states that in a function definition an empty set of parameters has a well-defined behaviour: that the function does not take any parameters. Thus such a definition for main matches the following description in the standard:
It [main] shall be defined with a return type of int and with no parameters.
There is, however, a noticeable difference between the two: namely, the version without void fails to provide a correct prototype for the function:
// this is OK.
int main()
{
if (0) main(42);
}
// this requires a diagnostic to be shown during compiling
int main(void)
{
if (0) main(42);
}
Oh, and just to be complete: the void has the following meaning in all function declarators:
(6.7.6.3p10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
First of all, there is a difference of what is allowed for hosted systems and freestanding systems, as shown here.
For hosted systems, 5.1.2.2.1 Program startup applies:
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)
... (more text follows regarding argv/argc etc styles).
The interesting part is "with no parameters". int main() and int main (void) are currently equivalent, since they are both function declarators and have no parameters. The following applies (6.7.6.3 ):
10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
/--/
14 An identifier list declares only the identifiers of the parameters of the function. An empty
list in a function declarator that is part of a definition of that function specifies that the
function has no parameters. The empty list in a function declarator that is not part of a
definition of that function specifies that no information about the number or types of the
parameters is supplied.145)
Emphasis mine, the bold text is what applies to int main(). There is also note 145) at the end of the text, which says "See ‘‘future language directions’’ (6.11.6)":
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
And here is the difference. Being a function declarator, int main() is bad style because of the above, since it is not guaranteed to work in the next version of the C standard. It is flagged as an obsolescent feature in C11.
You should therefore always use int main (void) on a hosted system and never int main(), even if the two forms are, for now, equivalent.
In C++ both forms are completely equivalent, but there int main() is the preferred style for subjective, cosmetic reasons (Bjarne Stroustrup says so... which is probably quite a bad rationale for explaining why you do something in a particular way).
In C, in a prototype (not in C++ though) an empty argument list means that the function could take any arguments (in the definition of a function, it means no arguments). In C++, an empty parameter list means no arguments. In C, to get no arguments, you have to use void. See this question for a better explanation.
In C++ having a function foo(void) and foo() is the same thing. However, in C it's different: foo(void) is a function that has no arguments, while foo() is a function with unspecified arguments.
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).
In C++, there is no difference between the two, and int main() is a legal signature and return type for main.
I know the thread is old but this question was bothering me for a while a few years ago so I wanted to throw in my half a cent(if that).
I always treat C functions as if they have fixed amount of arguments regardless of context, unless they use va_args. That is, I trust main to ALWAYS have the prototype:
int main(int argc, char **argv).
even if no arguments are passed, the function has these arguments on the stack because the main function does not have function overloading.
C does have the ability to have primitive overloading through just pretending the argument is not there. In which case, the argument is still passed and is on the stack but you never access it, so it merely reduces size of the source code.
Saying int main() simply means that I know that the function may have parameters, but I am not using them, so I write int main().
Saying int main(void) says that main CERTAINLY has no arguments, and implies that there are two different function prototypes:
int main(void);
int main(int argc, char **argv);
Since C has no function overloading, this is somewhat misleading to me, and I distrust code that has main(void) in it. I would not if main NEVER took any parameters, in which case main(void) would be completely OK.
NOTE: In some implementations, there are more parameters in main than argc and argv, such as env, but this does not bother me because I know that I do not explicitly say that those are the only two parameters, but those are the minimal parameters and it's okay to have more, but not less. This is in contrast to downright saying int main(void) which yells at me as THIS FUNCTION HAS NO PARAMETERS, which isn't true, since it does, they are just omitted.
Here is my basis code:
/* sample.c - build into sample. */
#include <stdio.h>
int main(void)
{
int _argc = *((int *)2686800);
char ***_pargv = (char ***)2686804;
int i;
for (i = 1; i < _argc; ++i) {
printf("%s ", (*_pargv)[i]);
}
return 0;
}
./sample I clearly have arguments
The function clearly has arguments passed to it, despite going out of the way to explicitly say that it doesn't by typing void into the function prototype.
As eq- says above:
(6.7.6.3p10) The special case of an unnamed parameter of type void as the
only item in the list specifies that the function has no parameters.
Thus saying that the function has void as an argument but actually having arguments on the stack is a contradiction.
My point is that arguments are still there, so explicitly asserting that main is void of arguments is dishonest. The honest way would be to say int main(), which claims nothing about how many parameters it has, only how many parameters you are care about.
NOTE2: The _argc, _pargv are system dependent, to find your values you must find them out by running this program:
/* findargs.c */
#include <stdio.h>
int main(int argc, char **argv)
{
printf("address of argc is %u.\n", &argc);
printf("address of argv is %u.\n", &argv);
return 0;
}
These values should remain correct for your specific system.
In C++, there is no difference between int main() and int main(void).
But in C they are little bit different.
int main() indicates that the main function can be called with any number of parameters or without any parameter. On the other hand, int main(void) indicates that the main function will be called without any parameter
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
Output: 4 3 2 1
#include <stdio.h>
int main(void)
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
It will show error. Because, in int main(void) parameter is void but in the program we have taken main(10) (which defines some value, not void)
Technically, if your host is partially POSIX compliant, then you have
int main(); // this legacy is always used by the run time library
int main(int argc); // illegal by compiler
int main(int argc, char** argv); // required by C standards
int main(int argc, char** argv, char** envp); // required by POSIX standard
If you have a Mac, there is also this
int main(int argc, char** argv, char** envp, char** apple); // required by Macintosh standard
Your host will send all the arguments, so a host will always send argc, argv, and envp (and apple if you are using an Apple product), but the programmer could have their main declared as taking void. The implicit function pointer typecast is technically an undefined behavior.
To prevent the typecast undefined behavior, int main() is a neutral form that means it could take any fixed number of arguments using canonical type promotion (int or larger, and double or larger) and int main(int argc, ...) means it could take any variable number of arguments also with canonical type promotion. In other words, the form return_type function_name() is an exception to undefined behavior.
In C++:
ㅤㅤint main() and int main(void) are the same in C++. They both take 0 and only 0 parameters.
In C:
ㅤㅤint main() takes as many arguments as you want. The function just won't use them. intㅤㅤㅤ main(void) makes it so passing an argument will create an error and make it impossible to ㅤㅤpass arguments.
ㅤ

Resources