473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why write return 0 at the end?

11
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <conio.h>
  3. int main ()
  4. {
  5.     cout << "Hello World";
  6.     getch();
  7.     return 0;
  8. }
  9.  
In the above code, why is it necessary to write getch() and return 0? What is their purpose?
Jan 22 '13 #1

✓ answered by donbock

getch() causes the program to pause at that point and wait for the user to press a key before the program proceeds to the return instruction and ends.

This is a common idiom when the programmer is concerned that the output window (the one that shows "Hello World") will be automatically closed when the program ends. It has nothing to do with the desired function of the program. Do you need this line? I don't know -- you should comment it out and see what happens.

By the way, notice that getch() and <conio.h> are not part of the language standard. Using them makes your program nonportable.

7 14602
Banfa
9,065 Expert Mod 8TB
the getch IMO is not at all necessary. People put it in because the do not understand the environment they are running there programs in and they run a command line program in a debug from there IDE. When run this way when the program exits the Window closes so they put in the getch so the program doesn't exit. If run not in debug mode or directly from Windows or from a command prompt, as a command line program is intended to be run, then the results remain on screen.

The return 0 is there because main returns a int which is the program exit code and an exit code of 0 indicates success.

In standard C++ this is not strictly required you can end main without a return statement and the return 0 is implicit, however since this code includes iostream.h which is not standard C++ (the standard header is iostream) I would have to assume you can't rely on it conforming to the standard so the return 0 is probably required.
Jan 22 '13 #2
MrError
11
I couldn't understand this (not getting the technical terms like command line program.!):
they run a command line program in a debug from there IDE. When run this way when the program exits the Window closes so they put in the getch so the program doesn't exit. If run not in debug mode or directly from Windows or from a command prompt, as a command line program is intended to be run, then the results remain on screen.
Jan 22 '13 #3
donbock
2,426 Expert 2GB
getch() causes the program to pause at that point and wait for the user to press a key before the program proceeds to the return instruction and ends.

This is a common idiom when the programmer is concerned that the output window (the one that shows "Hello World") will be automatically closed when the program ends. It has nothing to do with the desired function of the program. Do you need this line? I don't know -- you should comment it out and see what happens.

By the way, notice that getch() and <conio.h> are not part of the language standard. Using them makes your program nonportable.
Jan 22 '13 #4
as you are writing int main() that means it returns integer type of value so you need to return any integer value to main()
return(0) means return true or return successfully..

if you use void main() then dont use return(0)

Your program would be as follows
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>  
  2. #include <conio.h>
  3. void main ()
  4. {
  5.     cout << "Hello World";
  6.     getch();
  7. }
Jan 24 '13 #5
Banfa
9,065 Expert Mod 8TB
Yes of course void main() produces undefined behaviour and should be avoided. The only 2 definitions of main allowed by the C++ standard are

int main()
{
}

int main(int argc, char* argv[])
{
}

Anything else, such as returning void, is undefined behaviour which is to be avoided at all costs.
Jan 24 '13 #6
weaknessforcats
9,208 Expert Mod 8TB
Keep in mind that adding a getch() at the end of main() is usually for debugging purposes and is usually removed after the program is debugged.

For that reason, the above discussion does not apply to Microsoft Visual Studio. If you use this product, you do not put a getch() at the end of main(). Instead, when you run the program you select "start without debugging" rather than "start debugging".

Microsoft Visual Studio has two types of builds a "debug" build and a "release build". The difference is whether or not the compiler adds code to support the debugger in the executable.

When you have a debug build and you "start without debugging", the execution ignores all of your debug breaks, maybe including the one you put at the end of main() to pause your program. So the execution is stopped at the end of
main() with a "Press any key to continue..." added for your convenience.
Jan 24 '13 #7
divideby0
131 128KB
It doesn't affect this program, but getch is unbuffered or at least that's been my experience with it. It doesn't wait on "enter" being pressed; if any key exists in the keyboard buffer, it will read and return its value right away.

Expand|Select|Wrap|Line Numbers
  1. while(cin.get() != '\n'){}
Where "pausing" is actually desired, the above usually works for me, but my preference is to read all data into strings first and then convert as needed.
Jan 24 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Murdoch | last post by:
I have a situation where I call a COM object from ASP that is supposed to create a file. On occasion, especially during development, the output of the COM function is an error string rather than...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
25
by: guy | last post by:
i have inherited the following migrated vb6 code (vb2005) but i DONT get the "not all paths return a value" squiggly - is this a 'feature' of on error goto? Private Function CreateFolder(ByVal...
12
by: Abhishek | last post by:
now suppose I have declared an integer value inside a function as int x; now if the return type of the function is of type (void *) then can I write return((void *)x) in side the function? I...
4
by: Ed Sutton | last post by:
Is there an easy way to check using .NET 2.0? Thanks in advance, -Ed
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
6
by: ratheeshp | last post by:
Is it possible to write "return" function inside a conditional operator as shown below? a>b? return a: return b;
2
by: abhishekbrave | last post by:
I want to return true and false from the function below, does this function will automatically returns true or I have to write return statement public boolean connection() { Hashtable env = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.