"Hello World" without semicolon with a difference

J

Jeremy Yallop

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

I couldn't find where the standard says the expression is evaluated
for side effects, which is why I added the 'static' (which clearly
requires evaluation of the expression). I took the following to mean
that the expression is not evaluated:

5 If the size is an expression that is not an integer constant
expression: if it occurs in a declaration at function prototype
scope, it is treated as if it were replaced by *; otherwise, each
time it is evaluated it shall have a value greater than zero.

Jeremy.
 
A

ankursinha

I have no idea who Anirudha is???
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.

I am from India but this is not homework nor has anyone asked me this question
u may call me stupid but i read those classic interview questions(i agree such
questions shouldnt be asked at interviews) and i just thought whether such a
variant wsa possible.That's the reason behind such a question.
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

U may call it a fun challenge.
I'm not inclined to be helpful.

Thanx a lot.Enjoy Life.
 
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])
{
}

Shouldnt argv have only one size value in array declaration?

char *argv[] or char **argv

Also,could u pls explain the reason of using static.
 
K

Keith Thompson

I am from India but this is not homework nor has anyone asked me
this question u may call me stupid but i read those classic
interview questions(i agree such questions shouldnt be asked at
interviews) and i just thought whether such a variant wsa
possible.That's the reason behind such a question.


U may call it a fun challenge.


Thanx a lot.Enjoy Life.

Ok, if it's just a fun challenge, that's perfectly fine with me. In
the future, I suggest making that more explicit, just to avoid
possible misunderstandings. We do get a lot of people coming here
asking us to do their homework for them; if you ask a question that
looks like it could be homework, it's a good idea to provide some
background.

I'm glad to learn that my suspiciouns were unfounded. I'd gladly jump
into the discussion (I like silly puzzles), but I don't have any ideas
or sufficient time to come up with any.

One piece of friendly advice: the abbreviation "u" for "you" is
generally frowned upon here. We discussed this at far too much length
not long ago, so I won't go into the reasoning; you can search the
archives, take my word for it, or ignore me if you wish.
 
A

August Derleth

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

When did `u r' become correct English?
 
J

Jeremy Yallop

Jeremy said:
Dan said:
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.

I couldn't find where the standard says the expression is evaluated
for side effects, which is why I added the 'static' (which clearly
requires evaluation of the expression). I took the following to mean
that the expression is not evaluated:

5 If the size is an expression that is not an integer constant
expression: if it occurs in a declaration at function prototype
scope, it is treated as if it were replaced by *; otherwise, each
time it is evaluated it shall have a value greater than zero.

I've just noticed that this doesn't apply because the VLA declaration
is not at function prototype scope.

Jeremy.
 
K

Kevin Handy

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

He's not ur, so he gets to use them. Only ur can't use them,
whoever he is.
 
O

Old Wolf

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.

Solution 1:

#error "Hello World"

(it isn't specified at what stage the text has to be "printed"
on the "screen")

Solution 2:

No.

(there might not be a screen)

Solution 3:

#include <stdio.h>
int main(void)
{
puts("Hello World");;
}

(only double semi-colons were used)
 
K

Keith Thompson

Keith Thompson said:
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.

Apparently there was no connection between the two articles, and it
was foolish of me to assume that there was. I saw two seemingly
similar posts, both from people with seemingly Indian names, and
assumed the likelihood of a connection. In fact, India is an
extremely large country, and my assumption was poorly thought out --
not unlike saying, "You must have met John Smith; he lives in
America." I apologize.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top