473,327 Members | 1,919 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,327 software developers and data experts.

int main and void main

17
Hi all..i wud b grateful if u cud explain to me when to use int main and void main, wat is the difference and when to use return 0 or 1 in c++..thx
Feb 10 '07 #1
4 3327
Ganon11
3,652 Expert 2GB
In C++, you can NEVER use a void main(). It must always be int main().

in C++, returning a 1 inside main indicates that there was an error. A common occurrence of this is when you are trying to input from a file, but the file cannot be loaded. It correctly exits the program, but with the added note that there was some error. Returning 0 indicates that everything worked just as planned.
Feb 10 '07 #2
iWillLiveforever
136 100+
actually you can use void main in C++ it just does not return anything i used it all the time in Turbo C++ if you do use it make sure to put a getch() at the end so the program pauses for the user to press a key
Feb 10 '07 #3
RRick
463 Expert 256MB
In the C world, you can use either the void or int format. Some C compilers issue a warning about the void main, but still compile the program.

The C++ world took a hard line approach to main. Now you get an error if you don't use int, end of story. I was surprised when I first saw it. I don't know why they took this hard line approach. Good intentions to force good coding? Who knows.

There is a 3rd approach: Don't define a return value for main, at all. The gnu c++ compiler spits out a warning but will compile it.
Feb 10 '07 #4
iWillLiveforever
136 100+
no you can use void main in some compilers i don't know about the new TC but in the old dos Turbo C++ you can use void main
Feb 10 '07 #5

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

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
7
by: Howard | last post by:
Hi all, with all the posts I see that have "void main()", and all the resulting corrections, I have to wonder...is there a commonly-used IDE out there that generates "void main()" by default...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
1
by: BillZondlo | last post by:
Can someone look at this and tell me why, when I call this from my main app, it displays fine (fades form in and out) but then before my main app displays, I see other dialog boxes flash...
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
25
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
37
by: nick | last post by:
Does anyone know the file where the main function is defined? thanks
80
by: Ioannis Vranos | last post by:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int main(int argc, char *argv)"? AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv) - and the **argv...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.