difference between main function of c and c++

S

Stefan Ram

pankaj do it said:
what is the difference between the main() function of c and c++

The concept of »difference« only is defined for some
sets, such as the set of natural numbers.
For example: 4 - 3 = 1. So this difference is »1«.

But what is the difference between the moon and freedom?

For functions of different programming languages,
the concept of »difference« is not generally defined.
 
M

Marcel Müller

pankaj said:
what is the difference between the main() function of c and c++

There is no difference. Both have the same signature. Both use C linkage.


Marcel
 
S

Stefan Ram

Marcel Müller said:
There is no difference.

The following text is a program complying with the ISO
standard of one of the two languages mentioned, but not
of the other:

int main(){ main(); }

Also the »void« in

int main( void ){}

is recommended in one of the two languages, but not the other.
 
J

James Kanze

There is no difference. Both have the same signature. Both use
C linkage.

In C++, the linkage of main is implementation defined. It might
be "C++", it might be "C" or it might be something else
entirely.
 
J

James Kanze

Isn't this largly determined by the startup code as a
practical matter? If so, it seems that basically it's
implementation defined even in c.

Formally, the C++ or C program doesn't start until main has been
reached, but practically, yes... an implementation will
obviously have to define main in some way that it can be called
via the start-up code. (But of course, the start-up code is
part of the implementation.) As another poster pointed out, C
also allows main to be called (recursively) from within the
program, which introduces some additional constraints on it.
It's probably just a personal limitation, but I can't imagine
a reason to call main from anything other than startup code.

There are some interesting examples of using main recursively in
the IOCCC, but that's about it as far as I'm concerned as well.
Seems like any attempt to do so would need to diddle
implementation defined environment stuff to be effective. To
use Kernighan's phrase, am I suffering from a "poverty of
imagination"?

I can imagine any number of special things you could do by
calling main recursively. In every case, however, I can also
imagine a solution which wouldn't involve calling main
recursively which would be considerably more understandable and
easier to maintain.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top