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

strange case of void main

Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}

--
Jazz is not dead, it just smells funny.
(frank zappa)

Nov 14 '05 #1
7 1690
The only problem i see here is that ( as you have already put it), this
does not conform to the latest spec. Try compiling on any of the latest
compilers, and they won't accept this.

Ziggy wrote:
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Nov 14 '05 #2
Ziggy wrote:
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}


Yes: main *must* be declared as an int-returning function, even if you
call exit to terminate your program.

Nov 14 '05 #3

"Ziggy" <d@d.net> a écrit dans le message de
news:c6**********@nic.grnet.gr...

Hi,
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?
#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}
It's an unspecified behaviour and an undefined behavior in an hosted
environment (annexes J.1 and J.2) with the current standard. Nevertheless,
the behavior can be defined by the implementation. I let you find a copy of
the standard (C99, ISO/IEC 9899:1999) to see the definitions and the
differences between these terms, especially concerning the behavior's topic.

Having a draft of C99, I would recommend to read carefully §5.1.2 (execution
environments)
At program startup:
void main() isn't of the recommend form: it shall be int main(void) or int
main(int argc, char *argv[]) , §5.1.2.2.1
....But it could be another form defined by the implementation, see annex
J.3.2
Let's come back to §5.1.2.2.3, now about program termination, in your case,
main doesn't return anything (the return type is incompatible with int), the
termination status of the program returned to the host environnement is
unspecified.
Finally, by looking at the exit() function in §7.20.4.3, if the status
(parameter) of exit is EXIT_SUCCESS or 0, an implemention-defined form of a
successful termination is returned.

All that to say, rather than digging in a compiler documentation to obtain a
*correct* program whose the behavior is well-defined thanks to
implementation specific features, the best way would probably here to follow
the standard :)

Regis

--
Jazz is not dead, it just smells funny.
(frank zappa)

Nov 14 '05 #4
> The only problem i see here is that ( as you have already put it), this
does not conform to the latest spec. Try compiling on any of the latest
compilers, and they won't accept this.


Well, even the latest gcc "only" gives a warning about this:

test1.c: In function `main':
test1.c:2: warning: return type of `main' is not `int'

Nov 14 '05 #5
In article <40883c22$1@darkstar>,
Rakesh Kumar <dr******************@yahoo.com> wrote:
The only problem i see here is that ( as you have already put it), this
does not conform to the latest spec. Try compiling on any of the latest
compilers, and they won't accept this.

Ziggy wrote:
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}


The biggest problem with the code is that it identifies the author as a
fool who either doesn't know the C language, or believes that standards
don't apply to him or her.

The problems can range from not compiling, which is really the best
thing that can happen, to returning a different status code than
expected, to crashing, to damaging things, and of course damaging the
authors reputation. All that is easily avoided by using int instead of
void.
Nov 14 '05 #6
In <c6**********@nic.grnet.gr> Ziggy <d@d.net> writes:
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}


In C89 it's unconditional undefined behaviour. In C99 it's undefined
behaviour, unless the implementation *explicitly* defines this form of
main definition.

In the real life, if such code works, it works by accident, not by design.
The DEC C for VMS version I tried several years ago would not even
compile it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Dan Pop wrote:
In <c6**********@nic.grnet.gr> Ziggy <d@d.net> writes:

Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?

#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}

In C89 it's unconditional undefined behaviour. In C99 it's undefined
behaviour, unless the implementation *explicitly* defines this form of
main definition.

In the real life, if such code works, it works by accident, not by design.
The DEC C for VMS version I tried several years ago would not even
compile it.

Dan


Thanks everybody who replied.
Finally it seems to be one kind of implementation-defined feature.

peace

--
zig - zag - ziggy
Nov 14 '05 #8

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

Similar topics

10
by: Sony Antony | last post by:
I have the following simple program in Solaris Forte compiler 5.4 producing the warning. Though it produces the warning, it works fine as expected. This has been compiling fine without any...
13
by: Neil Zanella | last post by:
Hello, I wonder whether anyone has ever come across the following g++ compiler error message. I don't recall ever seeing it before. I solved my problem but I am still not sure about what this...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
6
by: Mårten Herberthson | last post by:
Introduction: As we all know, values of any class may be assigned to a reference of a superclass. This is simple polymorphism. So if you have a class A and a class B that inherits from A you can...
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
5
by: meLlamanJefe | last post by:
I have written a Queue class for our use that stores pointers to what I call RequestObjects. RequestObjects are a base class from which other types inherit and this allows the queue (or deque in this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.