473,756 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return value of main

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 standards defined "void" as return type
or is this issue just a lack of C knowledge?

Regards,
Chris
Aug 2 '06 #1
25 4510
Christian Christmann wrote:
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 standards defined "void" as return type
or is this issue just a lack of C knowledge?
The latter.

Try compiling void main(){} with gcc -Wall -ansi -pedantic.

--
Ian Collins.
Aug 2 '06 #2
? "Christian Christmann" <pl*****@yahoo. de?????? ??? ??????
news:pa******** *************** *****@yahoo.de. ..
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 standards defined "void" as return type
or is this issue just a lack of C knowledge?
Check questions 11.12a-11.15 of the C faq
(http://c-faq.com/ansi/).

Previous ANSI-C standards did not define void as return type.

--
Papastefanos Serafeim
Aug 2 '06 #3
Papastefanos Serafeim wrote:
? "Christian Christmann" <pl*****@yahoo. de?????? ??? ??????
news:pa******** *************** *****@yahoo.de. ..
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 standards defined "void" as return type
or is this issue just a lack of C knowledge?

Check questions 11.12a-11.15 of the C faq
(http://c-faq.com/ansi/).
Previous ANSI-C standards did not define void as return type.
Is this just sloppy posting, in which you meant "No ANSI C standard has
defined void as the return type for main", or is it
Jacob's disease of trying to take away features that have been part of C
since the *first* ANSI C standard (1989) striking again?

Check 3.1.2.5 of the 1989 C standard for the void type.

Aug 2 '06 #4
Christian Christmann <pl*****@yahoo. dewrites:
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 standards defined "void" as return type
or is this issue just a lack of C knowledge?
It's pure ignorance. No C standard has ever defined main as returning
void.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 2 '06 #5
Christian Christmann said:
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 standards defined "void" as return type
No, never, but see below.
or is this issue just a lack of C knowledge?
Mostly, yes.

The void type has never been a correct return type for main except in the
following circumstances, which also cover all other possible return types
(double main, struct tm main, union myunion main, etc):

1) in C90 and C99, if the implementation is freestanding and documents the
type in question as the appropriate return type for the entry point (which
might not even be called main);
2) in C99 *only*, if the implementation explicitly documents the return type
in question as being supported.

In each case, returning a value of a type other than int from main means
that your program is not portable to implementations that do not document
and support such a return type.

So, as with so much in life, the simple answer doesn't quite tell the full
story.

For maximal portability, use int as the type of your return value from main.
The only case where this doesn't work is where your (necessarily
freestanding) implementation specifically /requires/ you to do otherwise,
in which case your program - or at least the entry point part of it - isn't
going to be portable anyway.

A classic example of a freestanding implementation is Visual C when used in
the normal way to compile Windows programs - the entry point is not main,
but WinMain, and therefore the implementation *must* be freestanding! :-)

Ironically for a company that has done so much to spread the void main myth,
Microsoft's WinMain is required to return int!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 2 '06 #6
Richard Heathfield <in*****@invali d.invalidwrites :
Christian Christmann said:
>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 standards defined "void" as return type

No, never, but see below.
>or is this issue just a lack of C knowledge?

Mostly, yes.

The void type has never been a correct return type for main except in the
following circumstances, which also cover all other possible return types
(double main, struct tm main, union myunion main, etc):

1) in C90 and C99, if the implementation is freestanding and documents the
type in question as the appropriate return type for the entry point (which
might not even be called main);
2) in C99 *only*, if the implementation explicitly documents the return type
in question as being supported.
[...]

Both the C90 and C99 standards have the following:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

Allowing "void main(void)" would be such an extension. (Because of
this, C99 5.1.2.2.1's explicit permission to allow other forms of main
is, I believe, redundant.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 2 '06 #7
Keith Thompson <ks***@mib.orgw rote:
Allowing "void main(void)" would be such an extension. (Because of
this, C99 5.1.2.2.1's explicit permission to allow other forms of main
is, I believe, redundant.)
Redundant, but also perhaps yet necessary, given the relative
abundance of implementations offering that extension and the
consequent confusion.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Aug 2 '06 #8
Martin Ambuhl wrote:
Papastefanos Serafeim wrote:
? "Christian Christmann" <pl*****@yahoo. de?????? ??? ??????
news:pa******** *************** *****@yahoo.de. ..
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 standards defined "void" as return type
or is this issue just a lack of C knowledge?
Check questions 11.12a-11.15 of the C faq
(http://c-faq.com/ansi/).
Previous ANSI-C standards did not define void as return type.

Is this just sloppy posting, in which you meant "No ANSI C standard has
defined void as the return type for main", or is it
Jacob's disease of trying to take away features that have been part of C
since the *first* ANSI C standard (1989) striking again?
Given the context offered by the quoted material, the meaning of
Papastefanos's statement is clear. The same cannot be said for
your statement about "Jacob's disease."

Aug 2 '06 #9
In article <11************ **********@m79g 2000cwm.googleg roups.com>,
Dingo <di************ *@aol.comwrote:
....
>Given the context offered by the quoted material, the meaning of
Papastefanos 's statement is clear. The same cannot be said for
your statement about "Jacob's disease."
Shhhh. If you disagree with a "regular", you risk being branded a,
gasp!, "troll".

Aug 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3436
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 the main after submit?
32
8869
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 of "return 0;". He explained that "return" is not a function but a stament, like I didn't know already. The other colleagues also argreed with him :(. Can someone please explain what's so wrong about using "return" with
16
1996
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 see how the return value comes to play here. Ex
20
3608
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
37
3484
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 rundos.exe, it says the
17
1979
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 through the numbers and find the perfect numbers and list their factors. I cannot use pointers or a table array or formulae. And I need to have a seperate function from main doing all of this.
2
3086
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 textbox value in the main page is undefined ---------------------------------------- here are code main page: ------------------------------------------- <script language="JavaScript"> var thedata; var newwin; var thenumber; function...
27
2505
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
2217
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, hence: exit(EXIT_FAILURE) exit (EXIT_SUCCESS) Bill
0
9455
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
10031
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
9708
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...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
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
6534
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
3805
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
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.