-1

I have just started with programming and had a question regarding main function , i read somewhere that we declare main function with a int data type and use and integer with the return statement, why can we not declare main function with a string data type and use a string as a return?

string main(){ return "hello";}

why is this code invalid ?

i have tried running this code in codeblocks and got an error error:

'string' does not name a type|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
ZiGaelle
  • 744
  • 1
  • 9
  • 21
  • 5
    Because the rules say you can't. See [here](https://en.cppreference.com/w/cpp/language/main_function) for the valid `main` signatures. I don't think it would have been impossible to enable`main` to return different data types, but they didn't allow that. – Blaze Jul 22 '19 at 07:07
  • The operating system dictates that a program can be called with string arguments, and expects an integer status code. C++ merely conforms to this dictate. – Amadan Jul 22 '19 at 07:10
  • The `main` function must be defined with an `int` return type. It returns to the system whether the program exited sucessfully. Returning 0 means success, another value is an error code. Other function can freely be defined with any return type or arguments. – tmlen Jul 22 '19 at 07:10
  • Think about what you do with that return value as well. It can't be used inside your program, only by the operating system. There are well-established conventions that most operating systems support integer error codes from programs, with `0` indicating success. Of course, there's no reason you couldn't write an OS to do something different, but changing widely-accepted conventions is hard, and a lot of programs that are currently cross-platform would not work on your system. – BoBTFish Jul 22 '19 at 07:11
  • Also `std::string` is a type only if `#include ` was included. To use `string` (without specifying the `std::` namespace), `using std::string;` or `using namespace std;` must be used. – tmlen Jul 22 '19 at 07:12
  • Here is everything you need to know: https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Piotr Siupa Jul 22 '19 at 07:36

1 Answers1

-1

Let's try to understand this question from C++ language designer's perspective. Now, if I were to allow the main method to return a string what memory implications or overheads would it add versus returning an int. Moreover, allowing string to be returned from a main poses issues related to memory leak. How much memory should be anticipated for the string return type (this will be determined at run time if string size is unknown during compile time)? And, whenever things need to be determined at run time it affects the performance. Imagine if this code were to be part of a mission critical embedded system (where there is a penalty for not meeting performance and reliability expectations) then what repercussions might it have. Also, using string could pose security threats by exposing to a wider attack surface.

Girish
  • 1
  • 2
  • 1
    I'm sorry, but neither does this answer the question, nor brings anything of value. Your assertions are mostly incorrect (e.g. return type size of `std::string`, embedded systems ever returning from `main` etc.), and unrelated to the question. – Bartek Banachewicz Jul 22 '19 at 07:59
  • @BartekBanachewicz, Can you please provide an answer that is a value addition to this question. – Girish Jul 22 '19 at 11:29
  • No, because this question is entirely answered by another question on SO. When encountering such an almost-duplicate, it's typically better to vote-to-close rather than write your own answer. I've closed this question myself, but even if you don't have such privilege, your vote-to-close or flag-to-close is the appropriate action to take. – Bartek Banachewicz Jul 22 '19 at 11:45
  • What do you mean by "Also, using string could pose security threats by exposing to a wider attack surface."? – Divyansh Jul 23 '19 at 03:48
  • @Divyansh. My reply doesn't answer the question you've posed, accurately. I have hinted on few peripheral issues and safe practices. So this may seem extraneous information. To answer your question on 'attack surface' --- It means that it is easier for the attacker to cause stack or memory overflow if the string is variable. In your case the string is fixed. So shouldn't be a problem. If the string was read from a stream (input, file, etc.) then without proper checks it is exposed to a potential attack. – Girish Jul 24 '19 at 06:28