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";
}
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 areturn
statement, the effect is that of executingreturn 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With