473,769 Members | 2,359 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
25 4512
"Rod Pemberton" <do*********@bi tfoad.cmmwrites :
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>But most of the reference you cite talk about the void type, not
specifically about "void main". Apparently ignorant uses of "void
main" are also older than I realized, but I see nothing indicating
that it was ever correct. Unless you can cite a standard or some
pre-ANSI de facto standard that says "void main" is valid, my point
stands.

'int' is a type-specifier. 'void' is a type-specifier. 'void main' has
'void' used as a type-specifier instead of 'int' as a type-specifier. So,
the key issue is how was 'void' used as a type-specifier _before_ (i.e.,
from 1979-80) it was standardized as part of the C language. As I
indicated, 1) 'void', on compilers that didn't supply it, was defined to a
valid type-specifier, usually 'int' and 2) it was added to an important Unix
compiler of the era PCC. If programmers of the era understood that 'void'
was usually an alias for 'int', it wouldn't be an ignorant use, would it?
It would only be ignorant if they continued to use it long after C90.
Programmers of that era would have understood that "void", when used
as a return type, indicates that the function doesn't return a value,
but that the word could be aliased to "int" for compilers that didn't
yet directly support "void".

Declaring
void main()
is and was no more sensible than declaring
void n = 42;

The usual way to declare main back in the old days was simply:
main()
which was used in the examples in K&R1.

--
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 3 '06 #21
"Rod Pemberton" <do*********@bi tfoad.cmmwrites :
"Dik T. Winter" <Di********@cwi .nlwrote in message
news:J3******** @cwi.nl...
>In article <11************ *@dscnews2.dccc d.edu"Rod Pemberton"
<do*********@bi tfoad.cmmwrites :
> "Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
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.

Chris Torek has covered this in the past... 'void main'
predates all formal C standards except K&R (1979). The links
below indicate 'void' was for the PCC compiler for 4.1c BSD
(about 1981).

Read that it is *not* about "void main"...
...
>So, most of your references are about the type void, not about
"void main".

You could search for other slightly later references too. I just
posted the earliest I could find. See also reply to KT.
The point is that the references you posted *don't support your
claim*, which was specifically about "void main", not just "void".

If you want to dig up references that actually do support your claim,
feel free to do so.

You can post all the examples you like of people *using* "void main";
that doesn't demonstrate that those uses are based on anything other
than ignorance. If you can find any references that support your
claim that using "void main" was based on anything other than
ignorance, I'd be interested in seeing them.

--
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 3 '06 #22
In article <11************ *@dscnews2.dccc d.edu"Rod Pemberton" <do*********@bi tfoad.cmmwrites :
....
'int' is a type-specifier. 'void' is a type-specifier. 'void main' has
'void' used as a type-specifier instead of 'int' as a type-specifier.
Yes, and 'double main' has 'double' as a type-specifier instead of 'int'.
And 'struct {float re, im;} main' has again some other type-specifier.
What is the relevance?
So,
the key issue is how was 'void' used as a type-specifier _before_ (i.e.,
from 1979-80) it was standardized as part of the C language.
Precisely as I wrote. To shut up lint complaining about unused function
returns.
As I
indicated, 1) 'void', on compilers that didn't supply it, was defined to a
valid type-specifier, usually 'int'
Yes, because you wanted to compile.
2) it was added to an important Unix
compiler of the era PCC.
Indeed, to be able to shut up lint.
If programmers of the era understood that 'void'
was usually an alias for 'int',
If they understood that, they did not understand it. 'void' as an alias
for 'int' would not shut up lint.
it wouldn't be an ignorant use, would it?
It would be ignorance, not knowing the reason 'void' was added.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 3 '06 #23
In most freestanding embedded systems main is called but never exited. void main (void) makes sense. If main returns a value where is the result going to be sent?

In most automotive systems for example processors either never stop or run without exiting all of the time the vehicle is on. Most automotive processors have a single standalone application almost uniformly written in C.

w..
Aug 3 '06 #24
Walter Banks <wa****@bytecra ft.comwrites:
In most freestanding embedded systems main is called but never
exited. void main (void) makes sense. If main returns a value where
is the result going to be sent?

In most automotive systems for example processors either never stop
or run without exiting all of the time the vehicle is on. Most
automotive processors have a single standalone application almost
uniformly written in C.
Please provide context when posting a followup.

You're right, I've been ignoring freestanding implementations . (We
tend to do that here, probably more than we should.)

C99 5.1.2.1p1:

In a freestanding environment (in which C program execution may
take place without any benefit of an operating system), the name
and type of the function called at program startup are
implementation-defined.

So in that context,
void main(void)
is as legitimate as
int main(void)
which is as legitimate as
unsigned long _Startup(int foobar)
The implementation gets to decide how the startup function is to be
declared. And if the program never returns to the environment (or
there's no environment for it to return to), then "void main(void)" is
very likely to be the most sensible way to define it.

So let me amend my previous comments to refer only to hosted
environments.

--
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 4 '06 #25
>In article <11************ *@dscnews2.dccc d.edu"Rod Pemberton"
><do*********@b itfoad.cmmwrite s:
>Chris Torek has covered this in the past... 'void main' predates all formal
C standards except K&R (1979). The links below indicate 'void' was for the
PCC compiler for 4.1c BSD (about 1981).
In article <J3********@cwi .nl>, Dik T. Winter <Di********@cwi .nlwrote:
>Read [the link] it is *not* about "void main"...
Indeed, it was about "void" itself. Of course if "void" were
simply "#define"d to "int", one could write:

#define void int
void main() { return 42; }

and the like; but this is equivalent to writing:

#define purple int
purple main() { return 42; }

Neither one is particularly sensible. If main() is to return an
"int" value, one might as well write "int main()" (or, in modern
C, "int main(void)"). (Of course one can add argc and argv as
usual.)

As an aside, "void" and PCC (Steve Johnson's Portable C Compiler)
long predate 4.1cBSD as well. The original BSD distributions were
on PDP-11s and related to V6 and V7 (Ken Thompson spent some time
at Berkeley, if I remember right) and at some point acquired the
PDP-11 version of PCC; the first VAX BSD distributions were derived
from 32V, which was sort of an offshoot of PWB Unix, and used PCC
from the beginning. Hence 3.0BSD, 4.0BSD, 4.1BSD, and both the
"a" and "b" versions of 4.1BSD (4.1a and 4.1b) used PCC along with
4.1c. The "a" through "c" versions were just various snapshots
along the way to the original 4.2BSD release, which was the first
"non-beta" release of sockets, fast file system, and so on.

(32V PCC had 8-character symbols as usual; I think it was somewhere
between 3.0 and 4.0BSD that Berkeley modified the compilers and
linkers to support longer names -- up to 1023 characters -- although
that may have been as late as 4.1BSD. This was probably mostly
for Berkeley Pascal, which strung nested function names together
with underscores. "Stabs" symbolic debugging was shoehorned in
shortly afterward, with Mark Linton's "dbx" debugger, which was
perhaps the first symbolic debugger for C code, although it was
originally part of the whole Pascal system.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 7 '06 #26

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

Similar topics

1
3437
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
8874
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
1997
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
3613
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
3487
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
1982
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
3087
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
2506
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
2220
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
9423
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
10210
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
10043
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...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
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
7406
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
6672
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
3956
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
3561
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.