473,902 Members | 4,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why write return 0 at the end?

11 New Member
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
7 14665
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Top Contributor
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
swapnali143
34 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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 New Member
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
2009
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 the file name. I'd like to display this error string, regardless of it's contents. Right now, depending on the error string, I get an error message about missing a semicolon or some such. I've tried using Encode and Server.HTMLEncode with no...
15
2822
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
25
2585
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 sFileName As String) As Boolean On Error GoTo Errors 'Create it if necessary If Directory.Exists(sFileName) = False Then
12
2637
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 came across this in a document on multithreading in C..I can post the exact portion of code which works correctly with such an assignment if you people want. Please clarify my doubt.I hope I have conveyed it properly.
4
5732
by: Ed Sutton | last post by:
Is there an easy way to check using .NET 2.0? Thanks in advance, -Ed
0
789
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14072
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
6
23016
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
1646
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 Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); ...
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11279
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10870
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4306
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.