3

What is the difference between main in a C program and in a C++ program?

Other than

  1. return statement ( default 1 in C,`0 in C++)
  2. syntax:

    int main() { /* … */ }
    int main(int argc, char* argv[]) { /* … */ }
    int main() ,  void main() ,etc ...     
    

Mainly:

  1. difference between main in C Program & C++ program

  2. Are there any differences between C++98, C++03 and C++0x according to the ISO standard? i.e program's entry point (program startup implementation), etc.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user751747
  • 1,129
  • 1
  • 8
  • 17
  • *return statement & parameters of main* — that's all there is to know and all that the standard can talk about – David Heffernan Aug 02 '11 at 11:37
  • That comment makes no sense to me. – David Heffernan Aug 02 '11 at 11:48
  • @David : thank Q for your answer , can u aable to answer about my 2nd question ,.... regarding c++98,c++03,c++0x is there any difference in main – user751747 Aug 02 '11 at 11:56
  • in c++ the return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signalled by a non-zero return .. – user751747 Aug 02 '11 at 12:02
  • 1
    the standard does not talk about such things – David Heffernan Aug 02 '11 at 12:15
  • 1
    Re "return statement ( default 1 in C ,0 in C++)": The default return in C98 is EXIT_SUCCESS, which is typically #defined to 0. – David Hammen Aug 02 '11 at 17:26
  • Isnt there a version of main which takes environment variables too? like `int main(int argc, char* argv[], char* env[])` or is it only for gcc? – balki Aug 04 '11 at 13:44

4 Answers4

10

In modern C, and modern C++:

  • main is always either int main() or int main(int, char*[]).
  • In C89, you have to return from main explicitly.
  • In C99 and C++, if you don't return explicitly, you implicitly return 0.

[(I've checked the C99 standard now and edited this paragraph.)] For your second question, in C99 you must have precisely one of the two main functions. In C++ the standard says that a program is well-formed if it has a main function that returns int, and that every conforming implementation must accept the two listed versions as an entry point (for a "hosted program", e.g. not for the Linux kernel); see 3.6.1. [/edit] To the best of my knowledge, calling conventions are also not part of the standard.

I don't understand your question about memory, but do note that neither C99 nor C++03 have anything but a rudimentary memory model, whereas the new C++0x explicitly adds a memory model in order to enable well-defined concurrent and atomic operations.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 3
    In c++, a conforming implementation can accept other different signatures for main, as long as it does accept those two. – David Rodríguez - dribeas Aug 02 '11 at 12:10
  • No, in all your three points. (1) for C you should note `int main(void)` since `int main()` has a different meaning in C than in C++. (2) In C99 you don't have to return explicitly from `main`. IIRC the return value defaults to `EXIT_SUCCESS`. (3) The implicit return value in C++ is `EXIT_SUCESS`, too, which in many cases is `0` but needs not to be so. – Jens Gustedt Aug 02 '11 at 15:52
  • @Jens: N3242 says in 3.6.1 that if there's no return statement, you get implicit `return 0`. I'll update the C99 one, though, thank you for pointing that out. – Kerrek SB Aug 02 '11 at 15:53
  • @Jens: What's the difference between `main()` and `main(void)`? – Kerrek SB Aug 02 '11 at 16:25
  • @Kerrek, in C an empty argument list is not a prototype but defines a function that takes an unspecified number of arguments. – Jens Gustedt Aug 02 '11 at 17:07
  • @Jens: I'm suspicious: C99 says, "An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters." Your statement is only true for declarations without definition, so it looks like if I say `int main() { }` I'm fine. – Kerrek SB Aug 02 '11 at 17:38
  • @Kerrek: `int main() { }` is a definition (and a declaration); `int main();` is a declaration; `int main(void);` is a prototype (and a declaration). – pmg Aug 02 '11 at 17:46
  • @David Rodríguez -- I think it's precluded by 3.6.1/2: *This function shall not be overloaded* – Gene Bushuyev Aug 03 '11 at 01:01
  • 2
    @Gene: Well, you can only have one `main` in any given program, but it can have arbitrary signature, as long as it returns `int`, and it's still a well-formed program. It might just not work on your platform. :-) – Kerrek SB Aug 03 '11 at 01:08
  • @Gene Bushuyev: Extending the same quote from the standard §3.6.1/2: *An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.* The *type* in the last sentence means the *arguments* to `main`. – David Rodríguez - dribeas Aug 03 '11 at 08:48
8

In C, as opposed to C++, main can be called recursively.

/* valid C */
#include <stdio.h>
int main(int argc, char **argv) {
  putchar(argc ? '.' : '\n');
  if (argc == 0) return 0;
  return main(argc - 1, NULL);
}
pmg
  • 106,608
  • 13
  • 126
  • 198
1

C99 and C++ are put in line for the definition of main in hosted environments. There are two function interfaces that are allowed

int main(void);
int main(int, char*[]);

Both languages allow the implicit return from main without return statement in which case a return value of EXIT_SUCCESS is returned to the caller.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 2
    Check out [this question](http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success): The implicit return is `0`, not `EXIT_SUCCESS`. – Kerrek SB Aug 02 '11 at 16:14
  • 2
    I can't speak for C, but C++ returns 0, not `EXIT_SUCCESS`. Both are treated as "successful" return values, though, for whatever that means to the implementation. – Dennis Zickefoose Aug 02 '11 at 16:16
1

Edit: Is there any difference in program startup implementation is there any difference in c++98,C++03,C+++0x main ,etc.........

Not in main. However, there is a huge difference in what happens before main is called in C versus C++. In C++, objects with static storage are typically initialized prior to entering main.

Note:
An implementation is allowed to perform dynamic initializations of static data in the midst of main, but it must do so prior to the first reference to that static data. I've never run across an implementation that takes advantage of this flexibility.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • it's pretty typical an implementation would defer the dynamic initialization until the first use, you just need to check optimized assembly code. – Gene Bushuyev Aug 03 '11 at 00:50