+ 4

What will happen if I say string main() instead of int main()

c++

1st May 2017, 7:49 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
13 Respostas
+ 11
Hmm no! You can't return a string from main(), it has to be an int. This will give you a compiler error.
1st May 2017, 7:57 PM
Karl T.
Karl T. - avatar
+ 10
Because there can be only two ways to terminate a program, succesfully or failure. A string here would not make sense.
1st May 2017, 8:05 PM
Karl T.
Karl T. - avatar
+ 8
@Sello, sure thing. But I think you should remove the best answer you gave to Aloisia. lol
1st May 2017, 8:10 PM
Karl T.
Karl T. - avatar
+ 8
Thanks!
1st May 2017, 8:17 PM
Karl T.
Karl T. - avatar
+ 5
Yeah it is because of the 0, it indicates that the end of the program
1st May 2017, 8:03 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 4
@Helioform you're right of course
1st May 2017, 8:01 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 4
you get the error that main has to return int, it's specificated like this
1st May 2017, 8:06 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 4
but it was a good question, see you got me there too :D
1st May 2017, 8:07 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 3
you have to return a string at the end of the function, usually there's return 0; at the end and now you would have to write something like return " ";
1st May 2017, 7:51 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 2
why can't you return a string , does that mean that main runs always as an int or is it because we need the int 0 at the end to safely terminates the program
1st May 2017, 8:00 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
+ 2
so if I say string main(name){ string name = "selby"; Cout << name; return name; } will I get a syntax error? if so why
1st May 2017, 8:04 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
+ 2
OK thank you, I understand now thanks again for the patient I appreciate it
1st May 2017, 8:06 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
+ 2
done lol
1st May 2017, 8:12 PM
Sello Selby Bopape
Sello Selby Bopape - avatar