One line of code won the 1987 International C language chaos code competition, and one line in 1987

Source: Internet
Author: User

One line of code won the 1987 International C language chaos code competition, and one line in 1987


Macb ()? Lpcbyu (& gbcq/_ \ 021% ocq \ 012 \ 0 _ = w (gbcq)/_ dak. _ =} _ ugb _ [0q60) s +


This is an interesting online puzzle created by CoolShell bloggers. Following the examples of front-end games that pass through the customs, they created several questions related to programmers and a customs clearance game, the second question of this test. And send "Advanced Programming for Unix environment (Third edition)" (thanks @ Guo Zhimin for sponsorship) or a mug (thanks to @ linux Command Line) to the top 10 who passed the customs )) these puzzles are interesting and difficult. Due to the limited level, I have not completed customs clearance, but I feel that these things are really interesting and worth doing. I can still learn a lot from them.

This is the link for the original game test: http://fun.coolshell.cn /. I think this test is quite interesting. When I first saw the page, I couldn't help it. Limited to personal abilities and narrow knowledge, I learned from the author that this is a field related to the Brainfuck programming language. After the Internet keyword search engine information, the answer is "“welcome.html ". The specific implementation process depends on Brainfuck-a program language that makes your mind messy




When we see this keyboard, we find that it is different from our normal keyboard layout. The question is to replace the keyboard character in the figure with the character at the corresponding position on the keyboard of our computer. So I did it quickly and the result is as follows:

main(){printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);} 
It's a bit dumpfounded here. Isn't that the so-called C language confusing code. This is 4th International Obfuscated C Code Contest (1987), written by David Korn of AT&T Bell Labs. Enjoy the works of various international C language chaotic code competitions. COPY the code and paste it into the IDE. The compilation prompt is "unix was notdeclared in this scope ". Okay, there is no declaration. Use the # define macro to define a value.
#define unix 1
Nima runs the result unix. This result is really unintentional, And the messy code is a word "messy.

By viewing relevant information, we finally figured out its true nature.


First, review a bit of knowledge:

Constant and ASCII character set. Each character occupies one byte in memory and is used to store its ASCII value. Therefore, C-language characters have numerical characteristics and can be computed like integers.

Escape characters: There are some characters, such as carriage return characters ('\ R'), line breaks (' \ n') and other control symbols. They cannot be displayed on the screen or input from the keyboard, only escape characters can be used. An escape character is composed of a backslash plus a character or number. It converts the character or number following the backslash to another meaning.

\ Ddd 1-3 octal characters, such as \ 102 = 'B' \ xhh 1-2 hexadecimal characters, such as \ x41 = 'A'
Printf ("% x", '\ n'); printf ("% x",' \ R'); // line breaks. the ASCII values of carriage returns are, d, that is, 10, 13 in decimal format.
Finally, it is very important.
Int a [3] = {1, 2, 3}; printf ("% d \ n", a [2], 2 [a]); // yes, your eyes are correct. The output is actually 3. </span>
From "C and pointer", arry [2] and 2 [arry] are equivalent. This strange technique is feasible because it is implemented in the following table in C language. Although there is no difference between the two, there is no doubt that this will affect the readability of the program. It is not recommended that large-scale promotion lead to unnecessary killing!

Here, we can convert the translated code again, as shown below:

Main () {printf (& unix ["\ 021% six \ n"], (unix) ["have"] + "fun"-0x60 );} // \ 021 = 17. The ASCII character is DC1. It is a character; \ 012 = 10 = 0xa = '\ n', \ 0 = '0', then main () {printf (& "DC1% six \ n \ 0" [unix], "have" [unix] + "fun"-0x60 );} // for unix = 1, main () {printf (& "DC1% six \ n" [1], "have" [1] + "fun"-0x60);} // & "DC1% six \ n", remove the address at subscript 1 in the string, then main () {printf ("% six \ n", 'a'-0x60 + "fun ");} // If the ASCII value of a is 97 = 0x61, main () {printf ("% six \ n", "fun" + 1);} is equivalent to main () {printf ("% six \ n", "un ");}
The entire process should be clear here. OK. Next, let's take a look at the next question.




Is there an international C language chaotic code competition?

This competition is now available. www.ioccc.org/main.html is their official website. Zhu juncheng is the first Chinese to win multiple titles!

Confusing C code

Contrary to software engineering, chaotic programming is intended to obscure the code, but can be compiled and run properly. There are also international chaotic programming competitions.
The "International C code chaos competition" started in 1984. A programmer sighed after reading the source code of the Bourne Shell in the 4.2BSD: "Oh, my God! This is terrible !" As a result, he proposed in USENET's newsgroup that if programmers intentionally write code in disorder, what exactly would the Code look like? [Sponsor Introduction] the first "C code chaos competition" was held ". The rule for that session is very simple, that is, to write the complete C code that is chaotic but can be compiled smoothly to the maximum extent within 512 bytes. Later, the competition continued. Because they are hosted on the Internet, there are a large number of participants and a wide range of participants. Finally, it became today's "International C code chaos competition" and a stage for the masses of "hackers" to show their programming skills and distinctive imagination.
For example:
Main (t, _, a) char * a; {return! 0 main (-86,0, a + 1) + a): 1, t <_? Main (t + 1, _, a): 3, main (-94,-27 + t, a) & t = 2? _ <13? <Br/> main (2, _ + 1, "% s % d \ n"): 9: 16: t <0? T <-72? Main (_, t, <br/> "@ n \ '+, # \'/* {} w +/w # cdnr/+, {} r/* de} +,/* {* +,/w {% +,/w # q # n +,/# {l +, /n {n +,/+ # n +,/#; # q # n +,/+ k #; * +,/\ 'r: \ 'd * \ '3 ,}{ w + K w \ 'K: \ '+} e #\'; dq # \ 'l q # \ '+ d \ 'K #! /+ K #; q # \ 'r} eKK #} w \ 'r} eKK {nl] \ '/#; # q # n \'){) #} w \ ') {nl] \'/+ # n \ '; d} rw \' I; #) {nl]! /N {n # \ '; r {# w \ 'r nc {nl] \'/# {l, + \ 'K {rw \ 'ik {; [{nl] \ '/w # q # n \ 'wk nw \' iwk {KK {nl]! /W {% \ 'l # w # \ 'I;: {nl] \'/* {q # \ 'ld; r \ '} {nlwb! /* De} \ 'C; {nl \ '-{} rw] \'/+, }##\' *} # nc ,\', # nw] \ '/+ kd \' + e} +; # \ 'rdq # w! Nr \ '/\')} +} {rl # \ '{n \') #} \ '+ }##(!! /& Quot ...... remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.