473,386 Members | 1,699 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.

Programs without semicolon

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
Nov 14 '05 #1
17 2303
subnet <su****@katamail.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #2
On Tue, 11 May 2004, subnet wrote:
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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
In <Pi*******************************@drj.pf> da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) writes:
On Tue, 11 May 2004, subnet wrote:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
> 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.
Nov 14 '05 #5
Pl**********@lycos.co.uk (PlasmaDragon) wrote:
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.


It compiles fine, but it returns garbage.

Richard
Nov 14 '05 #6
In <40****************@news.individual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Pl**********@lycos.co.uk (PlasmaDragon) wrote:
> 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.


It compiles fine, but it returns garbage.


Who says it shouldn't return garbage?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
> It compiles fine, but it returns garbage.

Richard


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.
Nov 14 '05 #8
On 12 May 2004 10:41:04 -0700, Pl**********@lycos.co.uk (PlasmaDragon)
wrote:
It compiles fine, but it returns garbage.

Richard


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);

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #9

On Wed, 12 May 2004, PlasmaDragon wrote:
[please don't snip attributions, PlasmaDragon]
It compiles fine, but it returns garbage.

Richard


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

<snip> 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
Nov 14 '05 #10
Alan Balmer <al******@att.net> wrote in message news:<mk********************************@4ax.com>. ..
On 12 May 2004 10:41:04 -0700, Pl**********@lycos.co.uk (PlasmaDragon)
wrote:
It compiles fine, but it returns garbage.

Richard


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.


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

--
Peter
Nov 14 '05 #11
In <Pi***********************************@unix41.andr ew.cmu.edu> "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <63**************************@posting.google.com > ai***@acay.com.au (Peter Nilsson) writes:
The behaviour is effectively undefined.


The behaviour is mereley defined by another document.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote ...
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].
--
Devarshi Chatterjee.
Nov 14 '05 #14
Devarshi Chatterjee wrote:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote ...
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.
Nov 14 '05 #15
In <7d**************************@posting.google.com > so*******@bluebottle.com (Devarshi Chatterjee) writes:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote ...
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16
"Dan Pop" <Da*****@cern.ch> wrote:
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.

--
Simon.
Nov 14 '05 #17
In <dc******************************@news.teranews.co m> "Ralmin" <ne**@ralminNOSPAM.cc> writes:
"Dan Pop" <Da*****@cern.ch> wrote:
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".


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18

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

Similar topics

0
by: Nikola Milutinovic | last post by:
Hi all. I'm not subscribed to the list, so all replies to me mail directly. I ran into a small problem, for which we have found a workaround. I was compiling PostgreSQL v7.4.1 and it's...
12
by: jrefactors | last post by:
If the C programs have UNIX system calls such as fork(), alarm(), etc.., we should call it UNIX programs, not traditional C programs? We couldn't compile the programs with system calls using VC++...
9
by: Jukka K. Korpela | last post by:
I noticed that Internet Explorer (6.0, on Win XP SP 2, all fixes installed) incorrectly renders e.g. &harr &euro &Omega literally and not as characters denoted by the entities, but if a semicolon...
33
by: ankursinha | last post by:
Hi, Is it possible to write a C program that prints "Hello World" on screen without having a single semi-colon in the entire program? The extra constraint here is that u r not allowed to use...
27
by: Jeremy Yallop | last post by:
Write a program that takes a C program in source form as input and prints the source code for a program with equivalent behaviour, but without semicolons, on standard output. Please note that...
2
by: muzammil azmi | last post by:
how can i compile/run a program without using semicolonin the program
3
by: Peter Michaux | last post by:
Hi, These first three links say that when reading document.cookie the name-value pairs are separated by semicolons and they show examples like "name=value;expires=date" where there is clearly...
2
by: kolla | last post by:
how to write a c programme to print a semicolon without writing using a semicolon in the main program
1
by: cybersuv | last post by:
Is There Any Way To Print "hallow" In C Without Using A Single Semicolon ... Please Help Me If Anyone Can...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.