By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

why this program can work??

P: n/a
Hi all,
I found the simple program below, no error, no warning. But it can work
with no output.
Does anyone know why?

int main()
{
0;
}

Apr 21 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

Scorpio wrote:
Hi all,
I found the simple program below, no error, no warning. But it can work
with no output.
Does anyone know why?

int main()
{
0;
}


What were you expecting the program to do ?

Apr 21 '06 #2

P: n/a
Scorpio kirjoitti:
Hi all,
I found the simple program below, no error, no warning. But it can work
with no output.
Does anyone know why?

int main()
{
0;
}

Why could it not work? It has no syntax errors. Having a lone 0 in the
line is no error...

Aki Tuomi
Apr 21 '06 #3

P: n/a
On 2006-04-21, Scorpio <im********@gmail.com> wrote:
Hi all,
I found the simple program below, no error, no warning. But it can work
with no output.
You should get a warning, because main() is declared as returning int
but doesn't have any return statements in it.
Does anyone know why?

int main()
{
0;
}


In C any expression can be used as a statement, including the expression
"0". It doesn't do anything, but it's still a valid statement.

The sort of flip side of this is why you can do things like embedded
assignments in C:

if ((a = 10) == b)
...

This works because <a = 10> is an expression just like any other
(although it's often used on its own as a statement-- unlike <0>, which
is usually used as part of a more complex expression).
Apr 21 '06 #4

P: n/a

"Scorpio" <im********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi all,
I found the simple program below, no error, no warning.
It's a perfectly valid C99 program. With C89,
you'd need to add a 'return' statement.
with no output.
Does anyone know why?
The program has no statements which generate output.

int main()
{
0;
}


-Mike
Apr 21 '06 #5

P: n/a
In article <p3*****************@newsread3.news.pas.earthlink. net>,
Mike Wahler <mk******@mkwahler.net> wrote:
"Scorpio" <im********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googleg roups.com...
I found the simple program below, no error, no warning. It's a perfectly valid C99 program. With C89,
you'd need to add a 'return' statement.

int main()
{
0;
}


A return statement by itself is equivilent to falling off the end of
the main procedure in C89. A return statement with a value can make
a difference.

In C89, if one does not have a return statement, the status value returned
to the operating system is implementation defined. Failing to use
a return is not a violation of the standard; and the behaviour of
the operating system in response to any given status value is outside
of the standard.

The issue with return and C89 is not that it is required, but rather
that without it, one cannot be certain that the operating system will
be informed that the program was "successful". It is thus more robust
to remove the uncertainty by returning a value. The operating system
may still do Weird Things, but at least you would have given it the
maximum chance to do something useful.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Apr 21 '06 #6

P: n/a


Walter Roberson wrote On 04/21/06 12:00,:
In article <p3*****************@newsread3.news.pas.earthlink. net>,
Mike Wahler <mk******@mkwahler.net> wrote:

"Scorpio" <im********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.google groups.com...


I found the simple program below, no error, no warning.
It's a perfectly valid C99 program. With C89,
you'd need to add a 'return' statement.


int main()
{
0;
}

A return statement by itself is equivilent to falling off the end of
the main procedure in C89. A return statement with a value can make
a difference.

In C89, if one does not have a return statement, the status value returned
to the operating system is implementation defined. [...]


Actually, it's undefined. ANSI 2.1.2.2 "Hosted environment,"
final paragraph:

Program termination

[...] If the main function executes a return that
specifies no value, the termination status returned
to the host environment is undefined.

The implementation is obliged to document implementation-defined
behavior, but is allowed to remain silent about what kind of
termination status results when main() fails to return a value.
Some sort of status is returned -- the behavior is not entirely
undefined -- but the implementation needn't tell you what the
status might be.

--
Er*********@sun.com

Apr 21 '06 #7

P: n/a
"Scorpio" <im********@gmail.com> writes:
I found the simple program below, no error, no warning. But it can work
with no output.
Does anyone know why?

int main()
{
0;
}


Possibly the author of the program thought that, in the absence of a
return statement, main() would return the value of the last expression
evaluated. But I don't think any decent compiler would generate any
code for "0;", so the program should be exactly equivalent to:

int main()
{
}

(It's definitely equivalent as far as the standard is concerned; my
point is that I wouldn't expect the "0;" to have any effect even on
implementation-specific behavior.)

--
Keith Thompson (The_Other_Keith) 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.
Apr 21 '06 #8

P: n/a
Ben C wrote:
On 2006-04-21, Scorpio <im********@gmail.com> wrote:
Hi all,
I found the simple program below, no error, no warning. But it can work
with no output.

You should get a warning, because main() is declared as returning int
but doesn't have any return statements in it.


The compiler is not required to warn about this, though many do. In C89,
the return value is indeterminate. In C99, the return value is zero.

Simon.
Apr 22 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.