"Hello World" without semicolon with a difference

A

ankursinha

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 if,while,switch etc.

So far,i figured this could be done by insertint the printf statement in main
as shown:

int main(int argc=printf("Hello world")
{
}
Note:This is not allowed in ANSI C,default arguments are more a C++ concept.

Even this doesnt work.It compiles & runs but doesnt print anything.

Is the solution to this question possible in c(or c++ for that matter)?
 
R

Roberto Nunnari

I believe not unless you consider this a valid option:
#include <stdio.h>

#define WEE puts( "Hello World!" );

int main( int argc, char * argv[] ) {
WEE
}


...in fact to print on the screen you need an instruction.. and
in C every instruction has to be followed by a semi-colon.

Best regards.
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 if,while,switch etc.

So far,i figured this could be done by insertint the printf statement in main
as shown:

int main(int argc=printf("Hello world")
{
}
Note:This is not allowed in ANSI C,default arguments are more a C++ concept.

Even this doesnt work.It compiles & runs but doesnt print anything.

Is the solution to this question possible in c(or c++ for that matter)?


--
Roberto Nunnari -software engineer-
mailto:[email protected]
http://www.nunnisoft.ch
Residenza Boschetto 12 tel/fax: +41-91-6046511
6935 Bosco Luganese """ mobile: +41-76-3208561
Switzerland (o o)
========================oOO==(_)==OOo========================
 
E

Eric Sosman

ankursinha said:
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?

Answer #1: No, because the C language has no concept of
a "screen."

Answer #2: Yes, as follows

??=include <stdio.h>
int main(void) {
puts ("Hello, world!");;
return 10??'012;;
??>

Observe that there are no single semi-colons.
The extra constraint here is that u r not allowed to use if,while,switch etc.

In that case, the problem is impossible. (Assumption: "etc"
includes the open parenthesis character '('.)
So far,i figured this could be done by insertint the printf statement in main
as shown:

int main(int argc=printf("Hello world")
{
}
Note:This is not allowed in ANSI C,default arguments are more a C++ concept.

Even this doesnt work.It compiles & runs but doesnt print anything.

Is the solution to this question possible in c(or c++ for that matter)?

DYOH. Or if you won't DYOH, at least RTFW.
 
J

Jeremy Yallop

ankursinha said:
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 if,while,switch etc.

This invokes undefined behaviour, because variable length array types
aren't compatible with normal array types, but it's the closest I
could get. It's C99, obviously.

#include <stdio.h>
int main(int argc, char *argv[static printf("Hello World\n"), 0])
{
}

There's also

#error "Hello World"

but I don't consider that a program.

Jeremy.
 
T

Thomas Matthews

ankursinha said:
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 if,while,switch etc.

So far,i figured this could be done by insertint the printf statement in main
as shown:

int main(int argc=printf("Hello world")
{
}
Note:This is not allowed in ANSI C,default arguments are more a C++ concept.

Even this doesnt work.It compiles & runs but doesnt print anything.

Is the solution to this question possible in c(or c++ for that matter)?

The endless September:
http://groups.google.com/groups?hl=...n&btnG=Google+Search&meta=group=comp.lang.c.*

One should always consult the FAQ and search the newsgroups
before posting.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
G

Guillaume

Is the solution to this question possible in c(or c++ for that matter)?

You could try:


int main(int argc, char **argv)
{
if (printf("Hello world!\n")) {}
}


Fits the bill?
 
G

Guillaume

Damn, didn't remember you couldn't use 'if'.

I'm afraid there isn't such a beast, then...
 
R

Roberto Nunnari

Jeremy said:
#include <stdio.h>
int main(int argc, char *argv[static printf("Hello World\n"), 0])
{
}

hehehe.. this is CLEVER!

Could you please explain why that works? I mean.. why that printf
statement gets executed? Has it something to do with the fact
that static variables get initialized at start time?

Best regards.
There's also

#error "Hello World"

but I don't consider that a program.

Jeremy.


--
Roberto Nunnari -software engineer-
mailto:[email protected]
http://www.nunnisoft.ch
Residenza Boschetto 12 tel/fax: +41-91-6046511
6935 Bosco Luganese """ mobile: +41-76-3208561
Switzerland (o o)
========================oOO==(_)==OOo========================
 
D

Dan Pop

In said:
You could try:


int main(int argc, char **argv)
{
if (printf("Hello world!\n")) {}
}


Fits the bill?

When did the following statement disappear from his post?

The extra constraint here is that u r not allowed to use
if,while,switch etc.

Dan
 
D

Dan Pop

In said:
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 if,while,switch etc.

With this extra constraint, the solution involves cheating, one way or
another.

fangorn:~/tmp 470> cat test.c
#include <stdio.h>

int main()
{
puts("Hello World") _
}
fangorn:~/tmp 471> gcc -D_=\; test.c
fangorn:~/tmp 472> ./a.out
Hello World

Dan
 
K

Keith Thompson

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
if,while,switch etc.

Why would you want to do that? Are you and Aniruddha taking the same
class? (Aniruddha has been posting to comp.lang.c.moderated asking
how to access command line arguments without using argc and argv, and
how to emulate sizeof without using sizeof.)
 
J

Joona I Palaste

Why would you want to do that? Are you and Aniruddha taking the same
class? (Aniruddha has been posting to comp.lang.c.moderated asking
how to access command line arguments without using argc and argv, and
how to emulate sizeof without using sizeof.)

Accessing command line arguments without using argc and argv is easy.
Just name the parameters something else. =)
 
K

Keith Thompson

Joona I Palaste said:
Accessing command line arguments without using argc and argv is easy.
Just name the parameters something else. =)

That was one of the proposed solutions over in clcm, but the Aniruddha
said it didn't fit the specification (without explaining why using
argc and argv wasn't acceptable).

I suspect some instructor in India is assigning these silly problems
without making it sufficiently clear that asking Usenet to do your
homework for you is not acceptable. (I'm not picking on India; of the
two blatant examples of this I've seen, both posters have
Indian-sounding names, one posted from a *.in address, and the other
used a hotmail address.) That's just speculation; there may be no
connection between the two posters, but the questions have the same
feel to them.

There may be legitimate reasons for these questions, or they may be
intended as fun but meaningless challenges (I have no problem with
that) but until I know *why* using the features of the language is not
an acceptable solution I'm not inclined to be helpful.
 
A

ankursinha

Jeremy Yallop said:
ankursinha said:
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 if,while,switch etc.

This invokes undefined behaviour, because variable length array types
aren't compatible with normal array types, but it's the closest I
could get. It's C99, obviously.

#include <stdio.h>
int main(int argc, char *argv[static printf("Hello World\n"), 0])
{
}

This code doesnt work(atleast on my pc).it says expression syntax &
array bounds misssing.
im using the old TurboC++ compiler.
also,isnt argv supposed to have only one array size value when u
declare it
as : char *argv[].

How is static printf ("Heloo") supposed to work.
It gives an error even if it is given as a normal statement.

static printf("hello");


There's also

#error "Hello World"

but I don't consider that a program.

Jeremy.

true.
 
J

James Hu

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 if,while,switch
etc.

This works on my system:

$ cat hello.c
# /*
echo Hello World
exit 0
# */
int main(void) {}
$ ./hello.c
Hello World
$ cc hello.c
$

-- James
 
R

R Pradeep Chandran

On 19 Feb 2004 20:32:21 -0800, in comp.lang.c, ankursinha wrote:
:> ankursinha wrote:
:> > 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 if,while,switch etc.
:>
<snip>
:> #include <stdio.h>
:> int main(int argc, char *argv[static printf("Hello World\n"), 0])
:> {
:> }
:
:This code doesnt work(atleast on my pc).it says expression syntax &
:array bounds misssing. im using the old TurboC++ compiler.

Get a new compiler. If you don't want to pay for one, get a free
compiler like gcc (part of cygwin), mingw or lcc. I don't know whether
any of these support C99 features yet. The latest version of gcc might.
On *nix systems, I think gcc supports many of the C99 features.

As for Jeremy's code, I am not sure how it works, but it does (with gcc
3.2.2). I think the whole expression within [] is treated as the size of
the array and since it is a comma separated expression, the LHS is
evaluated, result discarded and, the RHS is evaluated.

Someone please tell me if this is right. Also, why is static required
there? Confused...

Have a nice day,
Pradeep
 
D

Dan Pop

In said:
Why would you want to do that? Are you and Aniruddha taking the same
class? (Aniruddha has been posting to comp.lang.c.moderated asking
how to access command line arguments without using argc and argv, and
how to emulate sizeof without using sizeof.)

Well, emulating sizeof is an enlightening exercise for anyone who has
taken a course on basic C programming, while the current problem only
has a sensible solution in C99.

Dan
 
D

Dan Pop

In said:
Accessing command line arguments without using argc and argv is easy.
Just name the parameters something else. =)

That's the point: in his example, main() was defined without arguments!

Dan
 
D

Dan Pop

In said:
Jeremy Yallop said:
ankursinha said:
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 if,while,switch etc.

This invokes undefined behaviour, because variable length array types
aren't compatible with normal array types, but it's the closest I
could get. It's C99, obviously. ^^^^^^^^^^^^^^^^^^^

#include <stdio.h>
int main(int argc, char *argv[static printf("Hello World\n"), 0])
{
}

This code doesnt work(atleast on my pc).it says expression syntax &
array bounds misssing.
im using the old TurboC++ compiler.
^^^^^^^^^^^^^^^^
Are you reading impaired or merely a patent idiot?

Dan
 
D

Dan Pop

In said:
On 19 Feb 2004 20:32:21 -0800, in comp.lang.c, ankursinha wrote:
:> ankursinha wrote:
:> > 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 if,while,switch etc.
:>
<snip>
:> #include <stdio.h>
:> int main(int argc, char *argv[static printf("Hello World\n"), 0])
:> {
:> }
:
:This code doesnt work(atleast on my pc).it says expression syntax &
:array bounds misssing. im using the old TurboC++ compiler.

Get a new compiler. If you don't want to pay for one, get a free
compiler like gcc (part of cygwin), mingw or lcc. I don't know whether
any of these support C99 features yet. The latest version of gcc might.
On *nix systems, I think gcc supports many of the C99 features.

As for Jeremy's code, I am not sure how it works, but it does (with gcc
3.2.2). I think the whole expression within [] is treated as the size of
the array and since it is a comma separated expression, the LHS is
evaluated, result discarded and, the RHS is evaluated.

Someone please tell me if this is right.

Yes, argv is defined as a VLA (variable length array). It is not clear
if C99 allows the second parameter of main() to be defined this way, so
the code is not necessarily correct.
Also, why is static required there? Confused...

It's another C99 feature, but it is not needed here. Without static,
things would be even simpler:

int main(int argc, char *argv[printf("Hello World\n")]) {}

because the value used in the VLA definition is completely ignored after
being evaluated for any side effects, the effective type of argv being
char **, as usual.

The big point, however, is whether C99 allows the second parameter of
main() to be defined as a VLA.

Dan
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top