Programs without semicolon

S

subnet

I'm not a C expert, but I've seen this topic discussed in the
newsgroup several times. What's the deal with writing such programs?
Why are they considered so interesting?

Thanks
 
J

Joona I Palaste

subnet said:
I'm not a C expert, but I've seen this topic discussed in the
newsgroup several times. What's the deal with writing such programs?
Why are they considered so interesting?

Beats me. Maybe some professor is handing out such assignments like
they're going out of fashion, which I certainly hope they are.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 
D

Darrell Grainger

I'm not a C expert, but I've seen this topic discussed in the
newsgroup several times. What's the deal with writing such programs?
Why are they considered so interesting?

It is hard to do without getting creative. The idea is that you really
have to know the language and have a bit of creativity to be able to write
such a program.

The other thought is that if you can write such a program you 1) know a
lot about the language and 2) you can recall the appropriate information.

For example, everyone with a degree in Computer Science learned about
recursion. If I pose a challenge that requires recursion to solve it not
everyone will be able to figure it out even though they have all had
training on recursion. Having training is one thing. Using it effectively
is what employers are really looking for.

The truth of the matter is that many people are just good at using a
search engine or memorizing questions that people like to use in
interviews.

One other aspect of these questions is that once I know what the answer is
I can impress my friends by asking them the same question and watch them
struggle with finding the answer.

The specific challenge of writing a program that has no semicolons comes
from the fact that everyone is taught that:

int main(void)
{
return 0;
}

is a minimal program. You start with this and build from here. If this
really is the simpliest program and there is already one semicolon in it,
how do you get rid of the semicolon?

int main(void)
{
return 0
}

This will not compile. You actually have to make the code more complex to
be able to remove the semicolon.
 
D

Dan Pop

In said:
It is hard to do without getting creative. The idea is that you really
have to know the language and have a bit of creativity to be able to write
such a program.

Not really. You know from start that your main function can't contain
any statement or declaration, except the ones of its two parameters.
There is nothing useful you can do in the declaration of argc, so your
only remaining hope is argv. After this trivial analysis, the solution
becomes obvious to anyone familiar with C99.

The C89 reasoning is a bit more complex, but still no big deal:
The only statement that doesn't require a terminating semicolon is the
empty block statement. So you can make your function call in the
controlling expression of an if or while statement that conditionally
executes an empty block statement. The while statement even allows
programming loops.

Dan
 
P

PlasmaDragon

int main(void)
{
return 0
}

This will not compile. You actually have to make the code more complex to
be able to remove the semicolon.

No, you will have to make it simpler:

int main(){}

compiles fine on GCC.
 
P

PlasmaDragon

It compiles fine, but it returns garbage.

Returns fine for me. Here's a test program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int x;
x=system("nosemicolon");
printf("%d",x);
printf("\n\n");

system("PAUSE");
}

Where "nosemicolon" is the name of your program that doesn't have a
semicolon. I always get the output 0.
 
A

Alan Balmer

Returns fine for me. Here's a test program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int x;
x=system("nosemicolon");
printf("%d",x);
printf("\n\n");

system("PAUSE");
}

Where "nosemicolon" is the name of your program that doesn't have a
semicolon. I always get the output 0.

What you're printing is the return value of system, which is
implementation-defined and may or may not reflect the return value of
nosemicolon.

BTW, why

printf("%d",x);
printf("\n\n");

rather than

printf("%d\n\n", x);
 
A

Arthur J. O'Dwyer

[please don't snip attributions, PlasmaDragon]
Returns fine for me. Here's a test program:
I always get the output 0.

Really? Prove it!

Test programs prove nothing, by definition. Besides,
you're arguing about a stupid quirk that's easily figured
out by consulting the Standards or a relevant FAQ.

In C89/C90/C95, falling off the end of 'main' returns
an implementation-defined value to the system.
In C99, falling off the end of 'main' is exactly equivalent
to returning 0 via 'return 0;' or 'exit(0);'.

This appears to be one of those cases where the C89 bigots
and the C99 bigots are simply butting heads for the heck of
it. If you guys would just say, "This answer refers to the
standard of 1989" and "This answer refers to the standard of
1999," threads like this one simply wouldn't happen.

-Arthur
 
P

Peter Nilsson

Alan Balmer said:
What you're printing is the return value of system, which is
implementation-defined and may or may not reflect the return value of
nosemicolon.

The behaviour is effectively undefined. Once you call system with a
non-null string, you potentially lose the guarantee of conforming
behaviour (assuming system even returns).
 
D

Dan Pop

In said:
In C89/C90/C95, falling off the end of 'main' returns
an implementation-defined value to the system.

Nope, the value is *undefined*.

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

Dan
 
D

Devarshi Chatterjee

Arthur J. O'Dwyer said:
Test programs prove nothing, by definition. Besides,
you're arguing about a stupid quirk that's easily figured
out by consulting the Standards or a relevant FAQ.

In C89/C90/C95, falling off the end of 'main' returns
an implementation-defined value to the system.
In C99, falling off the end of 'main' is exactly equivalent
to returning 0 via 'return 0;' or 'exit(0);'.

A reference to a section in the standards to substantiate
your claim(s) would be appreciated here.

ISO Section 5.1.2.2.3#1 starts as follows:
"If the return type of the main function is a type compatible
with int,..." [snipped for brevity]

Further on, ISO Section 6.8.6.4#1 says:
"A return statement with an expression shall not appear in a
function whose return type is void. A return statement without
an expression shall only appear in a function whose return
type is void."

CMIIW, but my limited intellect says that this implies that
'main' expects a return statement [or exit(...) for the pedants].
 
J

Jeremy Yallop

Devarshi said:
Arthur J. O'Dwyer said:
In C99, falling off the end of 'main' is exactly equivalent
to returning 0 via 'return 0;' or 'exit(0);'.

A reference to a section in the standards to substantiate
your claim(s) would be appreciated here.

ISO Section 5.1.2.2.3#1 starts as follows:
"If the return type of the main function is a type compatible
with int,..." [snipped for brevity]

... a return from the initial call to the main function is
equivalent to calling the exit function with the value returned by
the main function as its argument; reaching the } that terminates
the main function returns a value of 0.
 
D

Dan Pop

In said:
Arthur J. O'Dwyer said:
Test programs prove nothing, by definition. Besides,
you're arguing about a stupid quirk that's easily figured
out by consulting the Standards or a relevant FAQ.

In C89/C90/C95, falling off the end of 'main' returns
an implementation-defined value to the system.
In C99, falling off the end of 'main' is exactly equivalent
to returning 0 via 'return 0;' or 'exit(0);'.

A reference to a section in the standards to substantiate
your claim(s) would be appreciated here.

ISO Section 5.1.2.2.3#1 starts as follows:
"If the return type of the main function is a type compatible
with int,..." [snipped for brevity]

You should have read the text you've snipped, because this is what it
says:

... a return from the initial call to the main function is
equivalent to calling the exit function with the value returned
by the main function as its argument;10) reaching the } that
^^^^^^^^^^^^^^^^^^^
terminates the main function returns a value of 0.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Further on, ISO Section 6.8.6.4#1 says:
"A return statement with an expression shall not appear in a
function whose return type is void. A return statement without
an expression shall only appear in a function whose return
type is void."

CMIIW, but my limited intellect says that this implies that
'main' expects a return statement [or exit(...) for the pedants].

Your intellect is too limited: nothing in 6.8.6.4#1 forbids a function
returning something to terminate without a return statement. And
5.1.2.2.3#1 *explicitly* documents what happens when a main defined as
returning int terminates without a return statement. If you're
curious about what happens in other such cases, the answer is provided
by 6.9.1#12:

12 If the } that terminates a function is reached, and the value
of the function call is used by the caller, the behavior is
undefined.

Therefore, the following is a strictly conforming C99 program, with a
well defined termination status. In C89, the termination status would be
undefined.

#include <stdio.h>

int foo(void) { printf("hello world\n"); }

int main() { foo(); }

I would expect a good compiler to emit a warning for each function
defined in this program, but the standard requires no diagnostic.

Dan
 
R

Ralmin

Dan Pop said:
If you're curious about what happens in other such cases, the answer
is provided by 6.9.1#12:

12 If the } that terminates a function is reached, and the
value of the function call is used by the caller, the
behavior is undefined.

Therefore, the following is a strictly conforming C99 program, with
a well defined termination status. In C89, the termination status
would be undefined.

My copy of C89 has equivalent wording in 3.6.6.4#4:

If a return statement without an expression is executed,
and the value of the function call is used by the caller,
the behavior is undefined. Reaching the } that terminates
a function is equivalent to executing a return statement
without an expression.

Following this logic, since the value of the function call to main is used
by its caller, the behaviour is undefined. I don't think there is such a
thing as an "undefined termination status". In this case, the program has
"undefined behaviour".
#include <stdio.h>

int foo(void) { printf("hello world\n"); }

int main() { foo(); }

I would expect a good compiler to emit a warning for each function
defined in this program, but the standard requires no diagnostic.

Agreed.
 
D

Dan Pop

In said:
My copy of C89 has equivalent wording in 3.6.6.4#4:

If a return statement without an expression is executed,
and the value of the function call is used by the caller,
the behavior is undefined. Reaching the } that terminates
a function is equivalent to executing a return statement
without an expression.

Following this logic, since the value of the function call to main is used
by its caller, the behaviour is undefined. I don't think there is such a
thing as an "undefined termination status". In this case, the program has
"undefined behaviour".

Why didn't you bother checking your copy of C89? You'd have learned that
there is such thing as "undefined termination status" and, therefore, the
program doesn't invoke undefined behaviour:

"Program termination"

A return from the initial call to the main function is equivalent
to calling the exit function with the value returned by the main
function as its argument. If the main function executes a return that
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
specifies no value, the termination status returned to the host
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
environment is undefined.
^^^^^^^^^^^^^^^^^^^^^^^^^

According to your own quote, falling off the end of main is the equivalent
to executing a return with no value.

Dan
 
Joined
Jun 1, 2007
Messages
1
Reaction score
0
subnet said:
I'm not a C expert, but I've seen this topic discussed in the
newsgroup several times. What's the deal with writing such programs?
Why are they considered so interesting?

Questions like these originate from fresh graduates or students in India who are preparing for interviews. Most of the interviewers here in India ask such stupid questions, they have no idea what programming is about.

I've maintain a FAQ website for C, and it contains many such questions and answers there. It is for programming forums in India, and hence such questions had to be put up in the FAQ. You can find the website here: http://prokutfaq.byethost15.com/CFAQ
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top