0

I am new to c++ and I was using code blocks . I found that main() function is not taking return type other than int and void . I tried other data type to return the value and the returned the value of the same data type used . It was giving me an error when i use return type other than void and int . I would really appreciate it if you could explain it to me.

  • 2
    it doesn't take void as the return type. And what's the point returning a non-integral type? [Where does the returned value for 'main' function go?](https://stackoverflow.com/q/25434850/995714) – phuclv Sep 29 '18 at 10:25
  • Could you provide a code example with error code? It could better tell us what you want do accomplish – pastaleg Sep 29 '18 at 10:27
  • 1
    Hint: Who/what gets the value returned by a program that terminates? Even if you could return a string or an array or some class, who will get it and what would you expect "who" to do with it? – 4386427 Sep 29 '18 at 10:33
  • In order for an application to be _executable_ on a particular operating system, the former should conform to the OS definition of what the _executable_ means. A programming language runtime abstracts the OS definition of _executable_ from the source code level replacing it with a language-level definition of _executable_. In C/C++ it's a free function returning int or void and taking nothing or argc/argv as arguments. The reason they chose these particular signatures to support is because it naturally maps to the POSIX OS/application interface. – bobah Sep 29 '18 at 10:37
  • 1
    @bobah "Returning int or void" is possibly true of some compilers, but not of "C/C++". In both C and C++, the only portable definitions of `main` are those equivalent to `int main(void);` and `int main(int, char**);`. C in a hosted environment allows additional implementation-defined types for `main`. C in a freestanding environment does not restrict the name `main`, if it is used at all. C++ always requires the return type to be `int` (if used at all, since a freestanding environment might not require one). – aschepler Sep 29 '18 at 11:04
  • @aschepler - I definitely learned couple of news from your comment, but my main point was that this all is _just_ a protocol between the application and the language runtime, a “given”. Asking about why it is not something else is probably not of much practicality. – bobah Sep 29 '18 at 11:09

1 Answers1

1

The main() function return value is the process exit status/code returned to the OS. Historically this exit code represents an integer with 0 meaning success (at least on Windows, DOS, and POSIX OS).

renick
  • 3,838
  • 2
  • 27
  • 38
  • But if I have other data type like char and returned char type value. In this case what it will represent success or failure. – user10433098 Sep 29 '18 at 11:11
  • 3
    @user10433098 -- why would you want to return a `char` or some other type from `main()`? You have to return what your operating system is looking for. If your OS is looking for some other type, that is where the implementation-defined caveat comes into play. – ad absurdum Sep 29 '18 at 11:22
  • 1
    @user10433098: `main` returns `int` because the language definition says so. That’s what the runtime environment is expecting. You do not own the interface to `main`, only its implementation. – John Bode Sep 29 '18 at 12:38