query about main()

O

onkar

sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...
 
A

Ancient_Hacker

onkar said:
sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...


The compiler probably doesnt have any idea that main() is special in
any way.
To the compiler it's just another function to compile.

You can verify this by calling main again from inside your program. it
should work just fine being called again (just watch out for infinite
recursion).

The ony way main is "special" is that you link most programs with a C
library, which contains some startup code which calls main() right
after a littl ebit of initializatio of globals and system items..
 
F

Flash Gordon

onkar said:
sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...

It's possible because the standard that defines the language mandates
the first two and some implementations define the third.

Why is char spelt char? Because the language definition says that it is.
 
A

ankisharma

onkar said:
sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...
because c standard says so....
As per ANSI spec 5.1.2.2.1, implementation declares no prototype for
the main function.....it says that it could be defined as int main
(void) or int main (int argc, char **argv).......
 
S

Simon Biber

The C standard requires implementations to support at least the
following two forms of main:
int main(void)
with no arguments, or the form:
int main(int argc, char *argv[])
with two arguments, the first being an int, and the second being a
pointer to pointer to char.

Implementations are free to allow other non-standard forms as well, such
as the one with char **env that you mention above. Code that uses a
non-standard form is not portable, as it will only work on
implementations that support it.
The compiler probably doesnt have any idea that main() is special in
any way.
To the compiler it's just another function to compile.

main is the name of the function that a hosted C implementation must
call when starting your program.

main is special in that it may have a differing number of arguments from
program to program. The compiler must arrange for the correct argument
values to be supplied when the argc/argv form is written, but also work
correctly when the void form is written.

No other C function must be called with a differing number of arguments
depending on how the function was defined.
 
O

osmium

onkar said:
sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...

Consistency is the hobgoblin of little minds.
-- Emerson

The people on the standards committee apparently felt the inconsistency with
normal calling conventions was worth the price.

Or else they hoped no one would notice.
 
C

Clever Monkey

onkar said:
sometimes main accepts int main() and sometimes int main(int
argc,char** argv) and sometimes int main(int argc,char **argv,char**
env) still the code complies properly How is this possible ??? Please
explan...
As others have mentioned, the standard allows for some different
definitions of main().

If you are asking why would the standard allows something like int
main(void), my assumption is that this is (at least partially) due to
conforming implementations that may have to be hosted on systems without
the notion of environmental or process arguments.
 
W

Walter Roberson

Clever Monkey said:
If you are asking why would the standard allows something like int
main(void), my assumption is that this is (at least partially) due to
conforming implementations that may have to be hosted on systems without
the notion of environmental or process arguments.

Except that C already provides for that by setting argc and argv to
particular values.

My answer would be closer to "It's historical and convenient". I
suspect that main() with no declared arguments long predates nailing
down exactly what should happen with argc and argv when the
environment is unable to provide arguments.
 
C

Clever Monkey

Walter said:
Except that C already provides for that by setting argc and argv to
particular values.

My answer would be closer to "It's historical and convenient". I
suspect that main() with no declared arguments long predates nailing
down exactly what should happen with argc and argv when the
environment is unable to provide arguments.
Sounds reasonable to me. I accept.
 
K

Keith Thompson

Simon Biber said:
Ancient_Hacker wrote: [...]
The compiler probably doesnt have any idea that main() is special in
any way.
To the compiler it's just another function to compile.

main is the name of the function that a hosted C implementation must
call when starting your program.

main is special in that it may have a differing number of arguments
from program to program. The compiler must arrange for the correct
argument values to be supplied when the argc/argv form is written, but
also work correctly when the void form is written.

No other C function must be called with a differing number of
arguments depending on how the function was defined.

Perhaps, but that's not quite how I think of it.

The fact that main() can be defined with a differing number of
arguments from one program to another doesn't make it special at all.
In fact, that's true of *any* function. I can define a function foo()
in one program that takes no arguments, and a function foo() in
another program that takes two arguments. Since they're really two
different functions that only happen to have the same name, there's no
reason their argument lists should have to match. It's the same with
main().

What's special about main() is that, first, an implementation can
*restrict* its arguments to one of the two forms described in the
standard (unlike any other user-defined function, which can be defined
any way you like), and, second, that the calling environment invokes
main() on program startup. (It happens that the environment has to
know how to invoke main() either with no arguments or with two
arguments. This is possible because the environment needn't be
implemented in portable C.)
 
K

Keith Thompson

osmium said:
Consistency is the hobgoblin of little minds.
-- Emerson

<OT>
The quotation is:
"A foolish Consistency is the hobgoblin of little minds."

(Perhaps I'm proving the point by mentioning this.)
</OT>
 
S

Simon Biber

Keith said:
What's special about main() is that, first, an implementation can
*restrict* its arguments to one of the two forms described in the
standard (unlike any other user-defined function, which can be defined
any way you like), and, second, that the calling environment invokes
main() on program startup. (It happens that the environment has to
know how to invoke main() either with no arguments or with two
arguments. This is possible because the environment needn't be
implemented in portable C.)

If the environment was implemented in C, and it implemented the program
startup call something like:
exit(main(argc, argv));

Then in the case that main was defined with no arguments, there would
technically be undefined behaviour.

I suppose it depends on the calling convention as to whether any special
clean-up must be performed if main did not accept the arguments passed
to it.
 
K

Keith Thompson

Simon Biber said:
If the environment was implemented in C, and it implemented the
program startup call something like:
exit(main(argc, argv));

Then in the case that main was defined with no arguments, there would
technically be undefined behaviour.

Yes, *if* that were the case, then there would be undefined behavior,
and the implementation would be non-conforming.

The calling environment does whatever is necessary to invoke main()
correctly with either signature (or with any other
implementation-defined signature). This can't be defined in portable
C, but the environment is absolutely required to handle it anyway.
I suppose it depends on the calling convention as to whether any
special clean-up must be performed if main did not accept the
arguments passed to it.

Probably.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top