473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How this one liner works ?


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

Nov 15 '05 #1
5 1897
g.***********@gmail.com wrote:
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] = b[a].

That's it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
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 Mair wrote:
g.***********@gmail.com wrote:
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] = b[a].

That's it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Nov 15 '05 #3
Hi

g.***********@gmail.com wrote:
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] = 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

Nov 15 '05 #4
Hi Markus,

Thank you very much.

Regards,
Raju

Markus Moll wrote:
Hi

g.***********@gmail.com wrote:
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] = 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


Nov 15 '05 #5
g.***********@gmail.com wrote:
Hi Markus,

Thank you very much.

Regards,
Raju

Markus Moll wrote:
Hi

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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: D. Alvarado | last post by:
Hello, Does anyone have a PHP 4 one-liner (or two-liner) for extracing a file from a directory in which I know the word "footer" is guaranteed to be in the file name, I know the precise directory...
8
by: Paul Watson | last post by:
Can a for loop be used in a one-liner? What am I missing? $ python -c "import sys;print ''.join()," now is the time now is the time $ python -c "import sys;for line in...
4
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
8
by: Mantorok Redgormor | last post by:
The only adder I was able to come up with for incrementing by one, uses a for loop. I was trying to do this without using a for loop while emulating i++(it's for obfuscated code) Anyone know...
3
by: srinivasan srinivas | last post by:
Hi, Do we have python one-liner like perl one-liner 'perl -e'?? Thanks, Srini Unlimited freedom, unlimited storage. Get it now,...
37
by: c.lang.myself | last post by:
Can you determine output of following one liner by visual inspection.. It had won an award in obfuscated C contest..... main() { printf(&unix,(unix)+"fun"-0×60);} You can also list your...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.