5

Is there any way to return string from main function in c++? i am mentioning the sample program below

string main(int argv, char* argc[])
{
  .
  .
  return "sucess";
}
kayle
  • 1,126
  • 13
  • 20
  • You may want to read: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Stefan Oct 07 '13 at 07:42
  • 3
    Why would you want to do that? Who will receive this string? – Asaf Oct 07 '13 at 07:43
  • 2
    How exactly do you intend to use that string ? – zakinster Oct 07 '13 at 07:43
  • Why not return 0 for success? – sara Oct 07 '13 at 07:44
  • The irony of returning "sucess" (verbatim) is somehow... telling. – WhozCraig Oct 07 '13 at 07:44
  • You can return `EXIT_SUCCESS`, so that you have the string "SUCCESS" in your *source code*... it looks good in presentations. – Kerrek SB Oct 07 '13 at 07:45
  • I am running the c++ binary from a shell script. i need to return the string to the shell script for further decision making. – kayle Oct 07 '13 at 07:46
  • See http://stackoverflow.com/questions/8844915/what-happens-if-main-does-not-return-an-int-value for the consequences... – sara Oct 07 '13 at 07:46
  • 1
    @kayle Then print on cout... That's what it's for – erlc Oct 07 '13 at 07:46
  • 1
    @kayle The shell is getting the [exit status](http://en.wikipedia.org/wiki/Exit_status) of the executed process, which can only be an integer. Use the [standard output](http://en.wikipedia.org/wiki/Standard_output) for that matter. – zakinster Oct 07 '13 at 07:48

5 Answers5

9

The standard says:

3.6.1 Main function [basic.start.main]

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. All implementations shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main. [...]

So no, you have to return an int.

From your comments on your post, you seem to want to simply output a string on the standard output, then you can do:

#include <iostream>
int main()
{
    std::cout << "Success" << std::endl;
    // No need for return 0; it is implicit in C++
}

As Kerrek SB said in the comments, the return 0; is implicit in C++ (standard § 3.6.1 /5):

[...] If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
4

Nope, It's not standard compliant. You have to return an int in order to show to the caller how the program exited (good, with an error ?).

And why would you return a string ?

Davidbrcz
  • 2,335
  • 18
  • 27
  • I am running the c++ binary from a shell script. i need to return the string to the shell script for further decision making. – kayle Oct 07 '13 at 07:44
  • For that, you might want to use the _output_ of the program, but it's also possible to convert the return value to some string in the script if you really want to. – Hulk Oct 07 '13 at 07:49
  • @kayle If you want to use the output of the program as text in a shell, you'll have to capture it in the shell. In bash, for example, invoke your program in `$(...)`.\ – James Kanze Oct 07 '13 at 08:49
4

No you can't, you must always return an int. Thinking more deeply, a string would be difficult for a shell program to deal with: when would the memory be released, what kind of string would be returned &c.)

However you can write to standard output or standard error using std::cout or std::cerr. That's the normal thing to do when you want a console application to output string-like data.

Typically, you then rely on your OS shell to direct the standard output of your program to the input of another one (unix uses the pipe | for that).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

You probably mean to write to the standard output:

#include <cstdio>

int main()
{
    // ...

    std::puts("success");

}   // no "return", success is implicit in C++

That's one of the standard way for processes to communicate data, and many tools are geared for being used in this way.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Why use puts in a c++ program? Use the standard cout stream! – erlc Oct 07 '13 at 07:47
  • 1
    @KerrekSB: Why no? Mind you, I'm not imposing or anything, just curious to know the reason. – legends2k Oct 07 '13 at 07:52
  • 1
    @legends2k: In this case, `std::puts` does precisely what I want, no more and no less; easy to read, and no hidden cost or default-constructed locales -- sometimes it feels nice to have something simple. I use iostreams when it suits, but I like to mix it up every so often. – Kerrek SB Oct 07 '13 at 07:57
  • @KerrekSB: Makes sense; there's no point in being a rigid fanatic of something without understanding what it really is/does. – legends2k Oct 07 '13 at 08:00
  • @legends2k: I really wish I could like iostreams better. I use them of course for `getline` and such, but there are just so many ugly and icky parts about them (e.g. the monstrous verbosity if you're trying to write a hex dumper, or the poor performance when formatting numbers), that I haven't managed to embrace them wholly in my mind yet... – Kerrek SB Oct 07 '13 at 08:03
  • @legends2k: (Not to mention the wholly cryptic "facets" part of the iostreams library, which I have never, ever seen put to good use. I'd love to, and I'd love to believe it's well designed, but that revelation hasn't come to me yet...) – Kerrek SB Oct 07 '13 at 08:14
  • Interesting, this preference for functions which don't work (or at least prevent the use of functions which work later). – James Kanze Oct 07 '13 at 08:45
  • @KerrekSB Whether you use `std::cout` or `stdout`, both will always be fully constructed. And I fail to see where `puts` is in anyway "clearer" than `std::cout <<`. Just the opposite: it's yet another function you have to learn. (What, for example, is the difference between `puts` and `fputs`? And why do some functions take the `FILE*` as first argument, and others as last?) – James Kanze Oct 07 '13 at 08:48
1

No, you can't return string from main in c++, cause return value from main function is process return code (0 if success, not 0 if process failed). So OS is waiting proper signal of ending from your app.

zaynyatyi
  • 1,116
  • 6
  • 18