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

in c the return value for main()

sanjay123456
125 100+
Dear Friend ,

in c the return value for main() is optional why in c++ the main() must return value ?

sanjay
Jan 27 '07 #1
2 2612
willakawill
1,646 1GB
Dear Friend ,

in c the return value for main() is optional why in c++ the main() must return value ?

sanjay
Hi sanjay
C and C++ have different ANSI standards. Both require an int to be returned from main() but the compiler will allow void in C. The runtime environment still expects an int to be returned so anything could happen. In C++ the compiler will fail if anything other than an int is declared. This is part of C++ standards.
Jan 27 '07 #2
Banfa
9,065 Expert Mod 8TB
In both C and C++ main MUST return int. Not doing so invokes undefined behaviour (it just happens that in many cases that the undefined behaviour is everything works as expected which is a possible outcome of undefined behaviour).

Undefined behaviour is a bad thing to invoke as anything could happen, the compiler is at liberty to create any code it chooses.

As willakawill states C++ compilers are just better at detecting this error but it does not mean that it is not an error in C.

main should be declared as

int main(int argc, char **argp);


In C99 and C++ you also have the option of declaring it as

int main(void);

but not in C89/C90
Jan 27 '07 #3

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

Similar topics

1
by: Piotr | last post by:
I have popup window (window.open) where I put any value in input field. After submit I wan to to return to the main window and get value from popup window. How to close popup window and return to...
32
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only 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...
37
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its...
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
46
by: Bill Cunningham | last post by:
I have heard that return -1 is not portable. So what would be the answer to portability in this case? What about exit(0) and exit (-1) or exit (1)? Or would it be best to stick with C's macros,...
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...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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.