Printing "hello , wolrd" with out using semicolon

  • Thread starter Prashanth Badabagni
  • Start date
J

Joona I Palaste

ASCII is off-topic in comp.lang.c. I think you meant to say,
"Do not assume ASCII."
#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z
&&P(%c%c%d\n),S,S,Z)&&f(++i,i)){}else if(--l==Z&&P(%c/\n),S
)&&f(++i,i)){}else if(exit(Z),Z){}}int main(){if(f(Z,Z)){}}

No fair with preprocessor trickery! When preprocessed, that code will
contain plenty of double quotes. If I'm allowed to assume ASCII, I can
write a program to do it containing double quotes only in standard
headers, even after preprocessing.
 
M

Martin Dickopp

Joona I Palaste said:
ASCII is off-topic in comp.lang.c. I think you meant to say,
"Do not assume ASCII."
#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z
&&P(%c%c%d\n),S,S,Z)&&f(++i,i)){}else if(--l==Z&&P(%c/\n),S
)&&f(++i,i)){}else if(exit(Z),Z){}}int main(){if(f(Z,Z)){}}

No fair with preprocessor trickery! When preprocessed, that code will
contain plenty of double quotes. If I'm allowed to assume ASCII, I can
write a program to do it containing double quotes only in standard
headers, even after preprocessing.

If I can assume ASCII, it's easy to do without using any preprocessor
facilities at all:

int main(void) { if (putchar((((main==main)+(main==main))*((main==
main)+(main==main)) * ((main==main)+(main==main)) * ((main==main)+
(main==main))* ((main==main)+(main==main)) * ((main==main)+(main==
main)))-(((main==main)+(main==main))*((main==main)+(main==main)))-
(main==main)), putchar((((main==main)+(main==main))*((main==main)+
(main==main)) * ((main==main)+(main==main)))+((main==main)+(main==
main))),exit(main!=main),main==main){}}

Martin
 
J

Joona I Palaste

Martin Dickopp said:
Joona I Palaste said:
Arthur J. O'Dwyer said:
Can anybody tell me how to print "hello,world" without using semicolon
Here is a variation: write C program to print ";"
without using semicolon, digits, double quote, and single quote.
Assume ascii.
ASCII is off-topic in comp.lang.c. I think you meant to say,
"Do not assume ASCII."
#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z
&&P(%c%c%d\n),S,S,Z)&&f(++i,i)){}else if(--l==Z&&P(%c/\n),S
)&&f(++i,i)){}else if(exit(Z),Z){}}int main(){if(f(Z,Z)){}}

No fair with preprocessor trickery! When preprocessed, that code will
contain plenty of double quotes. If I'm allowed to assume ASCII, I can
write a program to do it containing double quotes only in standard
headers, even after preprocessing.
If I can assume ASCII, it's easy to do without using any preprocessor
facilities at all:
int main(void) { if (putchar((((main==main)+(main==main))*((main==
main)+(main==main)) * ((main==main)+(main==main)) * ((main==main)+
(main==main))* ((main==main)+(main==main)) * ((main==main)+(main==
main)))-(((main==main)+(main==main))*((main==main)+(main==main)))-
(main==main)), putchar((((main==main)+(main==main))*((main==main)+
(main==main)) * ((main==main)+(main==main)))+((main==main)+(main==
main))),exit(main!=main),main==main){}}

That's pretty clever. My original program to print "Hello, world"
without using literal values was pretty much the same way, except it
used a static variable and its negation to form 0 and 1, and then use
those to form all other natural numbers. I dare you to write a "Hello,
world" program in the way you are doing above. It won't require
particularly ingenious thinking but it will be tedious to type out.
 
J

Jeremy Yallop

Martin said:
int main(void) { if (putchar((((main==main)+(main==main))*((main==
main)+(main==main)) * ((main==main)+(main==main)) * ((main==main)+
(main==main))* ((main==main)+(main==main)) * ((main==main)+(main==
main)))-(((main==main)+(main==main))*((main==main)+(main==main)))-
(main==main)), putchar((((main==main)+(main==main))*((main==main)+
(main==main)) * ((main==main)+(main==main)))+((main==main)+(main==
main))),exit(main!=main),main==main){}}

That's undefined behaviour: exit() is implicitly declared by the call
as a function returning int, but the standard function exit() is
defined as returning void.

Jeremy.
 
M

Martin Dickopp

Joona I Palaste said:
Martin Dickopp said:
Joona I Palaste said:
Arthur J. O'Dwyer <[email protected]> scribbled the following:
Can anybody tell me how to print "hello,world" without using semicolon
Here is a variation: write C program to print ";"
without using semicolon, digits, double quote, and single quote.
Assume ascii.

ASCII is off-topic in comp.lang.c. I think you meant to say,
"Do not assume ASCII."

#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z
&&P(%c%c%d\n),S,S,Z)&&f(++i,i)){}else if(--l==Z&&P(%c/\n),S
)&&f(++i,i)){}else if(exit(Z),Z){}}int main(){if(f(Z,Z)){}}

No fair with preprocessor trickery! When preprocessed, that code will
contain plenty of double quotes. If I'm allowed to assume ASCII, I can
write a program to do it containing double quotes only in standard
headers, even after preprocessing.
If I can assume ASCII, it's easy to do without using any preprocessor
facilities at all:
int main(void) { if (putchar((((main==main)+(main==main))*((main==
main)+(main==main)) * ((main==main)+(main==main)) * ((main==main)+
(main==main))* ((main==main)+(main==main)) * ((main==main)+(main==
main)))-(((main==main)+(main==main))*((main==main)+(main==main)))-
(main==main)), putchar((((main==main)+(main==main))*((main==main)+
(main==main)) * ((main==main)+(main==main)))+((main==main)+(main==
main))),exit(main!=main),main==main){}}

That's pretty clever. My original program to print "Hello, world"
without using literal values was pretty much the same way, except it
used a static variable and its negation to form 0 and 1, and then use
those to form all other natural numbers. I dare you to write a "Hello,
world" program in the way you are doing above. It won't require
particularly ingenious thinking but it will be tedious to type out.

I tend to use computers for boring and tedious tasks. :) Below is a
program which reads some text from standard input (until end-of-file
condition or an error is encountered) and writes to standard output a
C program in the spirit of the above (but without nice formatting).


#include <stdio.h>

int main (void)
{
int ch;
puts ("int main(void){if(");
while ((ch = getchar ()) != EOF)
{
puts ("putchar(");
if (ch == 0)
puts ("main!=main");
else
{
int parens = 0;
while (1)
{
if (ch & 1)
puts ("(main==main)");
if ((ch >>= 1) != 0)
{
puts ("+((main==main)+(main==main))*(");
++parens;
}
else
break;
}
while (parens-- > 0)
putchar (')');
}
puts ("),");
}
puts ("exit(main!=main),main==main){}}");
return 0;
}


Martin
 
M

Martin Dickopp

Jeremy Yallop said:
That's undefined behaviour: exit() is implicitly declared by the call
as a function returning int, but the standard function exit() is
defined as returning void.

Good catch. The only solution I can think of is to rely on the
"implicitly return 0 from main" behavior in C99.

Martin
 
A

Arthur J. O'Dwyer

That's pretty clever. My original program to print "Hello, world"
without using literal values was pretty much the same way, except it
used a static variable and its negation to form 0 and 1, and then use
those to form all other natural numbers. I dare you to write a "Hello,
world" program in the way you are doing above. It won't require
particularly ingenious thinking but it will be tedious to type out.

Let's hear it for program generators! (Yes, this was tweaked by
hand to get it nice and square. But the factoring of each ASCII value
and printing of all the 'main==main's was done by computer.)

-Arthur

int main(void){if(putchar(((((main==main)+(main==main))*((main==main)+
(main==main)+(main==main)+(main==main)))*(((main==main)+(main==main)+(
main==main))*((main==main)+(main==main)+(main==main)))))&&putchar(((((
(main==main)+(main==main))*((main==main)+(main==main)+(main==main)))*(
(((main==main)+(main==main)+(main==main))*(((main==main)+(main==main))
*((main==main)+(main==main)+(main==main))))-(main==main)))-(main==main
)))&&putchar(((((main==main)+(main==main)+(main==main))*((main==main)+
(main==main)+(main==main)))*(((main==main)+(main==main)+(main==main))*
((main==main)+(main==main)+(main==main)+(main==main)))))&&putchar(((((
main==main)+(main==main)+(main==main))*((main==main)+(main==main)+!!!(
main!=main)))*(((main==main)+(main==main)+(main==main))*((main==main)+
(main==main)+(main==main)+(main==main)))))&&putchar((((main==main)+!!!
(main!=main)+(main==main))*((((main==main)+(main==main))*(((!!!(main!=
main)+(main==main)+(main==main)+(main==main))*((main==main)+!!!(main!=
main)+(main==main)+(main==main)+(main==main)))-(main==main)))-!(main!=
main))))&&putchar((((main==main)+(main==main)+(main==main)+(main==main
))*((((main==main)+(main==main)+(main==main))*((main==main)+!!!(main!=
main)+(main==main)+(main==main)))-(main==main))))&&putchar(((!!(main==
main)+(main==main)+(main==main)+(main==main))*(((main==main)+!!(main==
main))*((main==main)+(main==main)+(main==main)+!!!!!(main!=main)))))&&
putchar((((((main==main)+(main==main))*((main==main)+(main==main)+!!!!
(main==main)+(main==main)))-(main==main))*((((main==main)+(main==main)
+(main==main))*(((main==main)+(main==main))*((main==main)+(main==main)
+(main==main))))-(main==main))))&&putchar((((main==main)+(main==main)+
(main==main))*((((main==main)+(main==main))*((((main==main)+!!!(main!=
main)+(main==main)+(main==main))*((main==main)+(main==main)+!!!(main!=
main)+(main==main)+(main==main)))-(main==main)))-(main==main))))&&!!!!
putchar(((((main==main)+(main==main))*((main==main)+(main==main)+(main
==main)))*((((main==main)+(main==main)+(main==main)+(main==main))*(!!(
main==main)+(main==main)+(main==main)+(main==main)+(main==main)))-!!!(
main!=main))))&&putchar(((((main==main)+(main==main)+(main==main))*(!!
(main==main)+(main==main)+(main==main)))*(((main==main)+(main==main)+(
main==main))*((main==main)+(main==main)+(main==main)+!(main!=main)))))
&&putchar(((((main==main)+(main==main))*((main==main)+(main==main)+!!(
main==main)+(main==main)+(main==main)))*(((main==main)+(main==main))*(
(main==main)+(main==main)+(main==main)+(main==main)+(main==main)))))&&
putchar((((main==main)+(main==main)+(main==main))*((((main==main)+!!!(
main!=main)+(main==main))*((main==main)+(main==main)+(main==main)+!!!(
main!=main)))-(main==main))))&&putchar((((main==main)+(main==main))*((
main==main)+(main==main)+(main==main)+(main==main)+(main==main))))){}}
 
J

Jeremy Yallop

Martin said:
Good catch. The only solution I can think of is to rely on the
"implicitly return 0 from main" behavior in C99.

If you use C99, though, you lose implicit declarations altogether, so
you need a declaration for putchar(), with a semicolon.

Jeremy.
 
M

Martin Dickopp

Jeremy Yallop said:
If you use C99, though, you lose implicit declarations altogether, so
you need a declaration for putchar(), with a semicolon.

Okay, I give up. :) There's no solution.

Martin
 
P

Peter Nilsson

Arthur J. O'Dwyer said:
Here is a variation: write C program to print ";"
without using semicolon, digits, double quote, and single quote.
Assume ascii.

ASCII is off-topic in comp.lang.c. I think you meant to say,
"Do not assume ASCII."

#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z

The \ in \n is not at the end of a line, not part of a character or
string literal, not part of a universal character, not part of an
include header, and does not constitute a punctuator or other
preprocessor token.

I know gcc does not issue a diagnostic, but unless I'm missing
something, it's a syntax error.
 
C

CBFalconer

Jeremy said:
That's undefined behaviour: exit() is implicitly declared by the call
as a function returning int, but the standard function exit() is
defined as returning void.

Nope. exit is defined as never returning, so there is no
behaviour to become undefined.
 
B

Ben Pfaff

CBFalconer said:
Nope. exit is defined as never returning, so there is no
behaviour to become undefined.

Nonsense. You are wrong for the same reason that anyone who
makes the same claim about main() is wrong.
 
A

Arthur J. O'Dwyer

Nope. exit is defined as never returning, so there is no
behaviour to become undefined.

You should know better than that! One of the possible effects of
undefined behavior is the computer's actually coming backwards in
time to mess up the program before it even gets to the trouble spot...

Suppose 'exit' is defined with a calling convention such that
it expected to see one parameter on the stack, followed by a return
address which it will use to print a diagnostic message to the
user if 'errno' is set. Suppose further that to preserve register
real estate, 'int' values are returned by the caller's allocating
some (write-only) space on the stack prior to pushing its return
address and any arguments to the function. (Note that variadic
functions must be *really* perverse on this implementation!)
Now, we call 'exit' as if it is a regular function returning an
'int'. We push our return address, then a write-only buffer for
the return value, and then the argument 0. 'exit' tries to read our
buffer as if it were an address, and we get an exception. Ta-da!

HTH,
-Arthur
 
C

CBFalconer

Ben said:
Nonsense. You are wrong for the same reason that anyone who
makes the same claim about main() is wrong.

But main will return sooner or later, and its return value will be
used somehow. This does not apply to exit.
 
B

Ben Pfaff

CBFalconer said:
But main will return sooner or later, and its return value will be
used somehow. This does not apply to exit.

Irrelevant. Read the standard. There is no difference between
the situations. Both instances are undefined.
 
C

CBFalconer

Arthur J. O'Dwyer said:
You should know better than that! One of the possible effects of
undefined behavior is the computer's actually coming backwards in
time to mess up the program before it even gets to the trouble spot...

Suppose 'exit' is defined with a calling convention such that
it expected to see one parameter on the stack, followed by a return
address which it will use to print a diagnostic message to the
user if 'errno' is set. Suppose further that to preserve register
real estate, 'int' values are returned by the caller's allocating
some (write-only) space on the stack prior to pushing its return
address and any arguments to the function. (Note that variadic
functions must be *really* perverse on this implementation!)
Now, we call 'exit' as if it is a regular function returning an
'int'. We push our return address, then a write-only buffer for
the return value, and then the argument 0. 'exit' tries to read our
buffer as if it were an address, and we get an exception. Ta-da!

We can agree on don't do dat, and that this whole thread is silly
and totally without redeeming features, but ...

Why can't we just use K&R source, not needing any prototypes, and
which has never heard the word 'void'? The need to handle such
prevents your mechanism above, IMO. Since the system is using
implicit int it must be C90, not C99.
 
R

Richard Bos

Arthur J. O'Dwyer said:
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z

The \ in \n is not at the end of a line, not part of a character or
string literal, not part of a universal character, not part of an
include header, and does not constitute a punctuator or other
preprocessor token.

Macro processing, including # stringising macros, is done in translation
phase 4; escape sequences are translated in phase 5. AFAICT this is
enough to ensure that the above is legal (at least in this regard!),
since by the time the backslash is seen, it _is_ part of a string
literal.

Richard
 
R

Richard Bos

Arthur J. O'Dwyer said:
#include <stdio.h>
#include <stdlib.h>
#define Z ((int)NULL)
#define P(x) printf(#x
#define L(x) #x
#define S L(| |)[!Z]
int f(int l,int i){if((i=l,!Z)&&l==Z&&P(%c%c%d\n),S,S,Z)&&f
(++i,i)){}else if(--l==Z&&P(\n))&&f(++i,i)){}else if(--l==Z
&&P(%c%c%d\n),S,S,Z)&&f(++i,i)){}else if(--l==Z&&P(%c/\n),S
)&&f(++i,i)){}else if(exit(Z),Z){}}int main(){if(f(Z,Z)){}}

Alas, there is another, quite serious, bug in this program... it invokes
undefined behaviour! It calls f(++i,i) four times, each time modifying
the object i and accessing it for another purpose without an intervening
sequence point - that's a separator, not a comma operator! Fortunately,
this can easily be solved by changing all of these calls to f(++i,l).

Richard
 
D

Daniel Haude

On Mon, 10 May 2004 22:12:34 +0200,
Martin Dickopp said:
Good catch. The only solution I can think of is to rely on the
"implicitly return 0 from main" behavior in C99.

How about (int)exit() ?

--Daniel
 
M

Martin Dickopp

Daniel Haude said:
On Mon, 10 May 2004 22:12:34 +0200,


How about (int)exit() ?

That doesn't help, and wouldn't have any effect anyway, as `exit'
doesn't return. The problem is that the behavior becomes undefined as
soon as an undeclared (or "implictly declared") function returning
anything but `int' is /called/.

Martin
 

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

Similar Threads

Hello everyone 0
Hello everyone ! 4
Hello Everybody 0
Hello I'm new to coding 1
bugs in c 20
Hello there ! 2
Any Ideas On This Simple Co-Prime program 1
Hello there 3

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top