Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returing string from main function in c++

Tags:

c++

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";
}
like image 307
kayle Avatar asked Nov 30 '22 03:11

kayle


1 Answers

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;
like image 67
Pierre Fourgeaud Avatar answered Dec 05 '22 06:12

Pierre Fourgeaud