How this one liner works ?

G

g.kanaka.raju

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

output : unix

I have printed &unix["\021%six\012\0"] ==> output : %six and
(unix)["have"]+"fun"-0x60 ==> output : un

Could anyone of you explain how the above works. I'm curious to know
the trick here. I really appriciate your thoughts.

Regards,
Raju
 
M

Michael Mair

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

output : unix

I have printed &unix["\021%six\012\0"] ==> output : %six and
(unix)["have"]+"fun"-0x60 ==> output : un

Could anyone of you explain how the above works. I'm curious to know
the trick here. I really appriciate your thoughts.

It invokes undefined behaviour and should produce a couple of
compiler warnings and errors.
You have forgotten to #include <stdio.h>.
unix is an unknown identifier. The program relies on unix having
either the value 0 or 1 (if unix is defined). If you want to be on
the safe side, replace unix by 1.
The program relies on \012 being something printable, probably
assumes ASCII.
Ugly detail: The \0 is very unnecessary.
It further relies on 0x60 being 'a'-1.
Then you add in a = b[a].

That's it.

Cheers
Michael
 
G

g.kanaka.raju

Hi Michael,

This program won a "Best One Liner" award in 1987. I found this in
"Communications of the ACM".

I'm trying analyzing the programming trick here.

Regards,
Raju

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

output : unix

I have printed &unix["\021%six\012\0"] ==> output : %six and
(unix)["have"]+"fun"-0x60 ==> output : un

Could anyone of you explain how the above works. I'm curious to know
the trick here. I really appriciate your thoughts.

It invokes undefined behaviour and should produce a couple of
compiler warnings and errors.
You have forgotten to #include <stdio.h>.
unix is an unknown identifier. The program relies on unix having
either the value 0 or 1 (if unix is defined). If you want to be on
the safe side, replace unix by 1.
The program relies on \012 being something printable, probably
assumes ASCII.
Ugly detail: The \0 is very unnecessary.
It further relies on 0x60 being 'a'-1.
Then you add in a = b[a].

That's it.

Cheers
Michael
 
M

Markus Moll

Hi

This program won a "Best One Liner" award in 1987. I found this in
"Communications of the ACM".

Well, Michael gave an explanation of the behaviour.
Notice however that 1987 was quite a while ago and C has changed since then
(C89/90, C99).
I'm trying analyzing the programming trick here.

There is no trick (well, the only one is that "unix" is a macro defined in a
UNIX environment (there, unix == 1))
Try compiling and running on windows...

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

As Michael pointed out, a = b[a]. It merely depends which one is the
pointer.
In this case, unix[str] should be read as str[unix], which gives the first
character of str. Therefore &unix["\021%six\012\0"] points to the '%' in
the string, i.e. to the string "%six\012\0". Similarly unix["have"] is 'a',
'a'-0x60 hopefully is 1 (not at all guaranteed) and finally "fun" + 1
points to "un".

hth
Markus
 
G

g.kanaka.raju

Hi Markus,

Thank you very much.

Regards,
Raju

Markus said:
Hi

This program won a "Best One Liner" award in 1987. I found this in
"Communications of the ACM".

Well, Michael gave an explanation of the behaviour.
Notice however that 1987 was quite a while ago and C has changed since then
(C89/90, C99).
I'm trying analyzing the programming trick here.

There is no trick (well, the only one is that "unix" is a macro defined in a
UNIX environment (there, unix == 1))
Try compiling and running on windows...

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

As Michael pointed out, a = b[a]. It merely depends which one is the
pointer.
In this case, unix[str] should be read as str[unix], which gives the first
character of str. Therefore &unix["\021%six\012\0"] points to the '%' in
the string, i.e. to the string "%six\012\0". Similarly unix["have"] is 'a',
'a'-0x60 hopefully is 1 (not at all guaranteed) and finally "fun" + 1
points to "un".

hth
Markus
 
D

Default User

Hi Markus,

Thank you very much.

Regards,
Raju


Don't top-post. Your reply belongs following or interspersed with
properly trimmed quotes. See, oh, the way everybody else around here
does it.




Brian
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top