why this program can work??

S

Scorpio

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;
}
 
D

dragoncoder

Scorpio said:
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 ?
 
A

Aki Tuomi

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
 
B

Ben C

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).
 
W

Walter Roberson

It's a perfectly valid C99 program. With C89,
you'd need to add a 'return' statement.

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.
 
E

Eric Sosman

Walter Roberson wrote On 04/21/06 12:00,:
It's a perfectly valid C99 program. With C89,
you'd need to add a 'return' statement.


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.
 
K

Keith Thompson

Scorpio said:
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.)
 
S

Simon Biber

Ben said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top