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) 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 **
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.
"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)
> 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'
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
...
|
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. ...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |