C ONE LINER

C

c.lang.myself

Can you determine output of following one liner by visual inspection..
It had won an award in obfuscated C contest.....


main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}


You can also list your favourite one liner...
 
P

Phil Carmody

Can you determine output of following one liner by visual inspection..
It had won an award in obfuscated C contest.....


main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

From inspection, it's clear that there is no output, apart from
the error message when compiling.

And even if you'd not fucked up the post, everyone saw the
original of that years ago. Would you stroll into a beatles
fan newsgroup and announce that Lennon was dead?

I hope you realise you're reinforcing the stereotype of
googlegroups posters being the bane of usenet.

Phil
 
G

gw7rib

Can you determine output of following one liner by visual inspection..
It had won an award in obfuscated C contest.....

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

Is "unix" supposed to be defined and, if so, as what?
 
M

Martien Verbruggen

Can you determine output of following one liner by visual inspection..
It had won an award in obfuscated C contest.....

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

Is "unix" supposed to be defined and, if so, as what?

It's the 1987 IOCCC entry by David Korn. Look it up with Google.

Martien
 
C

CBFalconer

Can you determine output of following one liner by visual
inspection. It had won an award in obfuscated C contest.

main() {printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

Is "unix" supposed to be defined and, if so, as what?

He says it is a one liner, so there is no way for it to be defined.
 
J

James Kuyper

CBFalconer said:
Can you determine output of following one liner by visual
inspection. It had won an award in obfuscated C contest.

main() {printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}
Is "unix" supposed to be defined and, if so, as what?

He says it is a one liner, so there is no way for it to be defined.

Giving the compiler the command line option "-Dunix=1" would make that
code work as expected on most of the C89 compilers I've ever used.

There were compilers that pre-defined 'unix' automatically. Such
compilers do not conform to any version of the C standard, but that
doesn't make them non-existent. It does make code which relies on their
non-conforming features off-topic.
 
C

CBFalconer

James said:
CBFalconer said:
Can you determine output of following one liner by visual
inspection. It had won an award in obfuscated C contest.

main() {printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}
Is "unix" supposed to be defined and, if so, as what?

He says it is a one liner, so there is no way for it to be defined.

Giving the compiler the command line option "-Dunix=1" would make that
code work as expected on most of the C89 compilers I've ever used.

Didn't think of that :)
 
K

Keith Thompson

Can you determine output of following one liner by visual inspection..
It had won an award in obfuscated C contest.....

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

Is "unix" supposed to be defined and, if so, as what?

Many years ago, Unix C compilers typically predefined the macro
"unix", so programmers could write:

#ifdef unix
/* Unix-specific code */
#endif

Modern C compilers aren't allowed to do that, since "unix" is in the
programmer's namespace, but the early compilers didn't make any such
guarantees.

The above code, (with "0?60" replaced by "0x60") is from the 1987
International Obfuscated C Code Contest (IOCCC); it was submitted by
David Korn, author of the Korn shell. If you want spoilers, go to
<http://www.ioccc.org/>.
 
J

James Kuyper

Keith said:
On 18 Nov, 11:29, "(e-mail address removed)" <[email protected]> ....
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}
....
The above code, (with "0?60" replaced by "0x60") is from the 1987

For all messages I've seen on this thread, that text displays as 0x60,
not 0?60, both from my Thunderbird newsreader and when using Firefox to
view the messages on Google Groups.
 
J

James Kuyper

Bill said:
James Kuyper said:
Keith said:
(e-mail address removed) writes:
On 18 Nov, 11:29, "(e-mail address removed)" <[email protected]> ...
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);} ...
The above code, (with "0?60" replaced by "0x60") is from the 1987

For all messages I've seen on this thread, that text displays as 0x60,
not 0?60, both from my Thunderbird newsreader and when using Firefox
to view the messages on Google Groups.

Mine is displaying it as the "times" symbol rather than the letter "x".

You're right, I missed that distinction - I can see the difference, now
that I know what to look for. I've just checked, and it displays as the
"times" symbol in all messages in this thread.
 
R

Richard Tobin

James Kuyper said:
For all messages I've seen on this thread, that text displays as 0x60,
not 0?60, both from my Thunderbird newsreader and when using Firefox to
view the messages on Google Groups.

It's a Latin-1 multiplication symbol, which looks very like a letter-x.

-- Richard
 
M

Martin Ambuhl

James said:
Keith said:
On 18 Nov, 11:29, "(e-mail address removed)" <[email protected]> ....
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}
....
The above code, (with "0?60" replaced by "0x60") is from the 1987

For all messages I've seen on this thread, that text displays as 0x60,
not 0?60, both from my Thunderbird newsreader and when using Firefox to
view the messages on Google Groups.

No, it doesn't. Firefox properly renders the non-ascii character in
"0×60" as '×', not 'x'.
 
K

Keith Thompson

Martin Ambuhl said:
James said:
Keith said:
(e-mail address removed) writes:
On 18 Nov, 11:29, "(e-mail address removed)" <[email protected]> ....
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);} ....
The above code, (with "0?60" replaced by "0x60") is from the 1987
For all messages I've seen on this thread, that text displays as
0x60, not 0?60, both from my Thunderbird newsreader and when using
Firefox to view the messages on Google Groups.

No, it doesn't. Firefox properly renders the non-ascii character in
"0×60" as '×', not 'x'.

It probably depends on how Firefox and the underlying OS are
configured, and what assumptions Firefox makes about the web page it's
trying to render.
 
K

Kevin D. Quitt

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}

Nowadays, it'd have to be a two-liner: the Standard (5.1.1.2 P 1, section
2) "A source file that is not empty shall end in a new-line character..."
 
K

Keith Thompson

Kevin D. Quitt said:
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0×60);}
Nowadays, it'd have to be a two-liner: the Standard (5.1.1.2 P 1, section
2) "A source file that is not empty shall end in a new-line character..."

How does that make it a two-liner?

You'd have to add a declaration for printf and make some other small
tweaks, but as long as you can persuade the compiler to predefine the
"unix" macro it should be doable in one line.

Here's my attempt. With all unnecessary whitespace deleted, it's 117
characters long, not counting the new-line.

int printf(const char*restrict format,...);int main(void){printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}

Or if you have problems with long lines:

int printf(const char*restrict format,...);
int main(void){printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}

Note that I'm assuming C99, so printf needs the "restrict" keyword and
the "return 0;" is implicit.
 
A

Andrey Tarasevich

Keith said:
You'd have to add a declaration for printf and make some other small
tweaks, but as long as you can persuade the compiler to predefine the
"unix" macro it should be doable in one line.

Why a macro? Why can't we just add a 'const int unix = 1;' declaration
into the very same single line?

According to what is written at IOCCC web site, 'unix' was not supposed
to be a macro at all (predefined or not). 'unix' was supposed to be an
implicitly declared automatic variable, implicitly defined as the first
and only variable in 'main'. Due to certain implementation specifics,
the "garbage" value stored in this variable originally was supposed to
be 1 (it was actually the 'argc' value prepared by the system). In other
words, in order to simulate the original idea as closely as possible
we'd have to simply add 'int unix;' right after the opening brace of
'main'. And if it fails to work properly, add an initializer.

BTW, why did the original author put a pair of braces around 'unix' in
'(unix)["have"]+"fun"-0x60'?
 
K

Keith Thompson

Keith Thompson said:
As recently as gcc 4.3.2 on Ubuntu, "unix" *is* predefined as a macro
with the value 1. I've never heard of ti being an implicitly declared
automatic variable.
[...]

Sorry, I meant to mention that "-ansi" turns this off. (And as long
as I'm correcting myself, "ti" should be "it".)
 
A

Andrey Tarasevich

Keith said:
That doesn't sound right at all. Do you have a URL for this
explanation of 'unix'?

I got it from here

http://www.di-mgt.com.au/src/korn_ioccc.txt

I have to admit that haven't noticed that this is not really IOCCC web
site at all. Quite possibly the explanation given at that page is
incorrect. It seemed pretty authentic to me and it made sense, at least
at the first sight. Now I wonder if this can actually work, given the
right OS.
 
K

Keith Thompson

Martien Verbruggen said:

That's David Ireland's analysis of David Korn's IOCCC entry, and I
think he just got it wrong. His expanded program declares "unix" as a
variable -- and his explanation of how it gets initialized to 1 when
the program is invoked with no arguments is plausible, at least for
some particular implementation.

In David Korn's original one-liner, "unix" is *not* declared; it can
work only if "unix" is declared as a macro.

Note that if you compile David Ireland's expanded program using gcc
(without using "-ansi" or "-std=c99", you'll get a syntax error, since
"int unix;" will expand to "int 1;".
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top