printf mystery

E

Esash

Hi All,

I came across this printf statement by David Korn who won an award in International Obfuscated C Code Competition (IOCCC). I couldn't really understand the logic. Please explain.

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

Thanks a lot, in advance.

Regards,
Esash
 
S

Shao Miller

Hi All,

I came across this printf statement by David Korn who won an award in International Obfuscated C Code Competition (IOCCC). I couldn't really understand the logic. Please explain.

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

Thanks a lot, in advance.

Try this:

http://www.ioccc.org/1987/korn.hint
 
S

Shao Miller

Hi All,

I came across this printf statement by David Korn who won an award in
International Obfuscated C Code Competition (IOCCC). I couldn't really
understand the logic. Please explain.

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

Thanks a lot, in advance.

Try this:

http://www.ioccc.org/1987/korn.hint

And if you're still stuck after that, this:

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

The "fun" thing about IOCCC is that the code is sometimes non-portable,
so the corresponding results are not portable.

If you're stuck trying to understand something, it can be handy to use a
"search" web-site, such as Google, which found the two web pages above,
within 1 second.
 
E

Esash

On 2/28/2013 23:17, Esash wrote:
Hi All,

I came across this printf statement by David Korn who won an award in
International Obfuscated C Code Competition (IOCCC). I couldn't really
understand the logic. Please explain.

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

Thanks a lot, in advance.
Try this:



And if you're still stuck after that, this:



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



The "fun" thing about IOCCC is that the code is sometimes non-portable,

so the corresponding results are not portable.



If you're stuck trying to understand something, it can be handy to use a

"search" web-site, such as Google, which found the two web pages above,

within 1 second.



--

- Shao Miller

--

"Thank you for the kind words; those are the kind of words I like to hear.



Cheerily," -- Richard Harter

Thanks a lot for the links. Maybe I overlooked the search results. Anyways, the links are great.
 
G

glen herrmannsfeldt

Esash said:
I came across this printf statement by David Korn who won an
award in International Obfuscated C Code Competition (IOCCC).
I couldn't really understand the logic. Please explain.
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-
0x60);}

As a start, try this one:

main() { printf("%d\n",unix);}

(at least on a unix-like system.)

"have"[1] is 'a', or 0x61,

"fun"+1 is "un"

If you figure out the format string for printf, you will have the
answer.

-- glen
 
P

Philip Lantz

Shao said:
Shao said:
Esash said:
I came across this printf statement by David Korn who won an award
in International Obfuscated C Code Competition (IOCCC). I couldn't
really understand the logic. Please explain.

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-
0x60);}
....
http://www.di-mgt.com.au/src/korn_ioccc.txt

Obviously unix is a macro with the value 1 when this is compiled on a
Unix system (as Korn intended), but the above web page (written by David
Ireland in 2002) seems to say that on a non-Unix machine declaring a
variable "int unix;" as the first thing in main will somehow cause that
variable to get the value of argc. I've never known a compiler to do
that. Did I completely misunderstand what Ireland was trying to explain,
or was there some compiler that would somehow associate the first
variable in main with the value of argc if argc wasn't declared?

Philip Lantz
 
N

Noob

Esash said:
printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);

Do the numbers chosen in the format string (21, 6, 12)
have any special meaning outside of C?

021 and 120 seem to have been chosen for the palindrome?
(Since the first character of the format string is ignored)
Something else?

Regards.
 
G

gwowen

Esash said:
printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);

Do the numbers chosen in the format string (21, 6, 12)
have any special meaning outside of C?

021 and 120 seem to have been chosen for the palindrome?

Ascii 021 (21 base 8 = 17) is special character.
%six is really "%s" "ix"
Ascii 012 (12 base 8 = 10) is the newline, I guess 21 is picked to
match that

The last bit is the real obfuscation, but your clue is

'a' = 97 and a ~ *(a+b)
 
N

Noob

gwowen said:
Esash said:
printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);

Do the numbers chosen in the format string (21, 6, 12)
have any special meaning outside of C?

021 and 120 seem to have been chosen for the palindrome?

Ascii 021 (21 base 8 = 17) is special character.
%six is really "%s" "ix"
Ascii 012 (12 base 8 = 10) is the newline, I guess 21 is picked to
match that

The last bit is the real obfuscation, but your clue is

Having "solved" the riddle, I note that the format string's
first character (i.e. '\021') is skipped over. Therefore,
the author could have chosen any character. Why did he pick
that one?

Also, I do know that %six is "%s""ix" (to get the program to dump
"unix") but he could have done it 666 other ways, so I was assuming
that his choice of "six" was relevant, somehow.

Why did he pick 021 six 120?

Or am I trying too hard to read into this?

Regards.
 
S

Shao Miller

Shao said:
Shao said:
Esash wrote:
I came across this printf statement by David Korn who won an award
in International Obfuscated C Code Competition (IOCCC). I couldn't
really understand the logic. Please explain.

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-
0x60);}
...
http://www.di-mgt.com.au/src/korn_ioccc.txt

Obviously unix is a macro with the value 1 when this is compiled on a
Unix system (as Korn intended), but the above web page (written by David
Ireland in 2002) seems to say that on a non-Unix machine declaring a
variable "int unix;" as the first thing in main will somehow cause that
variable to get the value of argc. I've never known a compiler to do
that. Did I completely misunderstand what Ireland was trying to explain,
or was there some compiler that would somehow associate the first
variable in main with the value of argc if argc wasn't declared?

That was the impression that I got from reading that article: That its
author believes that the undeclared 'unix' would be implicitly 'int' and
would somehow become an alias for 'argc'. Since it's non-portable code,
it doesn't particularly interest me. :) It doesn't seem too far-fetched
to suppose that a[n old] compiler might do this.
 
R

Rosario1903

Hi All,

I came across this printf statement by David Korn
who won an award in International Obfuscated
C Code Competition (IOCCC). I couldn't
really understand the logic. Please explain.

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

#include <stdio.h>

int unix;

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

the original not compile
the remain print:?ix
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top