C++ main function return type corrected

This C++ code looks correct… but it isn’t. Many beginners write something like this: --------- CODE --------- #include<iostream> usingnamespacestd; void main(){ cout<<"am I right?"; } --------- CODE --------- At first glance, nothing seems wrong. It compiles. It runs. It prints the output. So what's the problem? According to the C++ standard, the main() function must return int, not void. The correct version: --------- CODE --------- int main(){ cout << "am I right?"; return 0; } --------- CODE --------- Why does this matter? Because the return value of main() is sent back to the operating system to indicate the program's execution status. 0 → successful execution non-zero → program error Many compilers still allow void main() because they themselves return 0, which is why beginners often believe it’s correct. But writing standard-compliant code is what separates someone who just writes code from someone who understands the language. Small detail. Professional mindset. Did you also start your C++ journey with void main()? let me know in the comments. #cpp #programming #softwaredevelopment #coding #developers #learncoding

  • No alternative text description for this image

Honestly, I can't agree that using int vs void for main is what separates a professional from a non-professional developer. If it compiles fine, and runs as expected, it is OK. Because the main is just one item in the entire application (and depending on the kind of application we are writing, we don't even have a main). Also, if the problem is the correct main signature, then it must be: int main(int argc, char* argv[]) Also... main is usually auto-generated when you create a new project in most IDEs... so most people don't even remember the right signature.

You mean you don't do data validation?

Like
Reply

In the days of batch files that called compiled apps, and then checked the return value it was quite important.

Like
Reply

cout << "Hello World"; or printf("Hello World\n"); are the typical statements of a first C/C++ program.

THREE NOTES 1) The text "usingnamespacestd;" should be written as "using namespace std;" with two spaces but not as one token. 2) Talking about the Standard C++, use the language or quote from the Standard C++ or one of its drafts. I am attaching a specification for the function main. 3) In general, it is not a good style to define the C++ function main without a try block and catch handlers because a thrown but uncaught exception will terminate the application. Valerii Salov

  • No alternative text description for this image

I would not have complained if the compiler is happy with void main(). Thats a problem that is trivial to fix when changing compiler. I would have suggested a line break at the end of the printout. Some OS will not add any line break before they print a shell prompt, so a command line tool not finishing with a line break can result in a "prompt": Hello World!my-machine:~$ " instead of Hello World! my-machine:~$

Like
Reply

The categorization of main() returning void is bad or somehow not professional is incorrect. As a C and C++ developer for more years than I care to say, you need to remember that an error return is an intentional behavior. If there is no way to infer from the return code whether or not something succeeded, then you should not use the return code. <rant> This brings up another problem, who interprets the exit code and how? In the linux shell you can inspect "$?" after a process exist. Yes, if it is "0" it is treated as success. This is great. Let's look at "grep" shall we? grep foo bar If the string "foo" exists in "bar" the exit code is 0. If the string "foo" does not exist in "bar" then the exit code is 1 If the file "bar" does not exist the exit code is 2. My biggest problem with command line exit codes is the linux shell. Many developers use "set -e" in their scripts that causes the script to exit immediately after an error. (ANY process returning an untested non-zero!) IMHO, "set -e" should never EVER be used. I have seen too many scripts that exit without any report of error because of something stupid like "grep." </rant>

no c++ compilers fully adhere to the language.heck if you use clang you can do range matching which will fail to compile under gcc.

According to the standard, int main() has an implicit return 0. See [basic.start.main]¶6. It is not new and has been there since well before C++11.

Most compilers insert a return 0 anyway even if you don’t do it , so it’s better to be explicit

See more comments

To view or add a comment, sign in

Explore content categories