454,952 Members | 1,416 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

# C ONE LINER

 P: n/a 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... Nov 18 '08 #1
37 Replies

 P: n/a On Nov 18, 11:29*am, "c.lang.mys...@gmail.com"

 P: n/a On 18 Nov, 11:29, "c.lang.mys...@gmail.com" wrote: 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? Nov 18 '08 #3

 P: n/a On Tue, 18 Nov 2008 15:12:42 -0800 (PST), gw****@aol.com wrote: >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 -- | Martien Verbruggen | Quick! Hire a teenager while they still know | everything. | Nov 19 '08 #4

 P: n/a gw****@aol.com wrote: "c.lang.mys...@gmail.com" Can you determine output of following one liner by visualinspection. 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. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Nov 19 '08 #5

 P: n/a CBFalconer wrote: gw****@aol.com wrote: >"c.lang.mys...@gmail.com" >Can you determine output of following one liner by visualinspection. 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. Nov 19 '08 #6

 P: n/a James Kuyper wrote: CBFalconer wrote: >gw****@aol.com wrote: >>"c.lang.mys...@gmail.com" Try the download section. Nov 19 '08 #7

 P: n/a gw****@aol.com writes: On 18 Nov, 11:29, "c.lang.mys...@gmail.com" wrote: >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 . -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 19 '08 #8

 P: n/a Keith Thompson wrote: gw****@aol.com writes: >On 18 Nov, 11:29, "c.lang.mys...@gmail.com" .... >>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. Nov 19 '08 #9

 P: n/a "James Kuyper" gw****@aol.com writes: >>On 18 Nov, 11:29, "c.lang.mys...@gmail.com" ... >>>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". That is, it's not the same character as the "x" in "unix" at the start of the line. If I save it and do a binary dump on it, it comes up as code 0xD7 rather than the 0x78 of a lower case "x". I assume Keith's news reader substitutes "?" when the character code is outside the standard ASCII set. - Bill Nov 19 '08 #10

 P: n/a Bill Leary wrote: "James Kuyper" Keith Thompson wrote: >>gw****@aol.com writes:On 18 Nov, 11:29, "c.lang.mys...@gmail.com" ... >>>>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 Firefoxto 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. Nov 19 '08 #11

 P: n/a In article , James Kuyper 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 toview the messages on Google Groups. It's a Latin-1 multiplication symbol, which looks very like a letter-x. -- Richard -- Please remember to mention me / in tapes you leave behind. Nov 19 '08 #12

 P: n/a James Kuyper wrote: Keith Thompson wrote: >gw****@aol.com writes: >>On 18 Nov, 11:29, "c.lang.mys...@gmail.com" .... >>>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'. Nov 19 '08 #13

 P: n/a Martin Ambuhl Keith Thompson wrote: >>gw****@aol.com writes:On 18 Nov, 11:29, "c.lang.mys...@gmail.com" .... >>>>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 as0x60, not 0?60, both from my Thunderbird newsreader and when usingFirefox 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. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 19 '08 #14

 P: n/a >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..." -- #include _ Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up Nov 19 '08 #15

 P: n/a 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..." 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. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 19 '08 #16

 P: n/a Keith Thompson Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 19 '08 #17

 P: n/a Keith Thompson wrote: Keith Thompson As recently as gcc 4.3.2 on Ubuntu, "unix" *is* predefined asa macro with the value 1. I've never heard of ti being animplicitly 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".) I imagine "-std=c99" will also shut it off. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Nov 20 '08 #18

 P: n/a ? Nov 20 '08 #19

 P: n/a "Chris M. Thomasson" 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... Where is: #include ? Implicit. Nov 20 '08 #20

 P: n/a Chris M. Thomasson said: "Chris M. Thomasson" >>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... Where is:#include ? Implicit. No, you were right the first time. It's missing. The behaviour of the program is consequently undefined (calling a variadic function without a valid function prototype in scope). A certain degree of latitude is often granted to IOCCC programs that would not be acceptable in a production environment. IOCCC is, after all, supposed to be a refuge for abused C code, n'est ce pas? -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Nov 20 '08 #21

 P: n/a Andrey Tarasevich

 P: n/a Richard Heathfield "Chris M. Thomasson" >>? Implicit. No, you were right the first time. It's missing. The behaviour of the program is consequently undefined (calling a variadic function without a valid function prototype in scope). A certain degree of latitude is often granted to IOCCC programs that would not be acceptable in a production environment. IOCCC is, after all, supposed to be a refuge for abused C code, n'est ce pas? Further allowances should be made for the fact that the code in question is from 1987, pre-C90, and the rules only asked for the code to conform to K&R ("plus common extensions"). Under K&R C, the use of printf would create the implicit declaration `extern int printf();' which would suffice. So the inclusion of stdio.h is not implicit, but the only relevant declaration that it contains, is. Nov 20 '08 #23

 P: n/a c.***********@gmail.com wrote: 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... There is no #include It will generate a compilation error saying printf is not defined. Nov 20 '08 #24

 P: n/a Nate Eldredge wrote: Andrey Tarasevich BTW, why did the original author put a pair of braces around 'unix' in(unix)["have"]+"fun"-0x60'? I assume it was to make it look like a cast. (Language barrier: to me, these characters ( ) are parentheses. Braces are these { } . These [ ] are brackets.) To you? Here, have an authoritative source: ISO/IEC 9899:1999 (E) ( )(parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5 [ ](brackets punctuator), 6.7.5.2, 6.7.8 { }(braces punctuator), 6.7.2.2, 6.7.2.3, 6.7.8, 6.8.2 -- pete Nov 20 '08 #25

 P: n/a In article

 P: n/a Michael said: c.***********@gmail.com wrote: >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... There is no #include It will generate a compilation error saying printf is not defined. The lack of a definition for printf isn't a problem for a compiler - the definition could easily be (and, in this case, is) in some other object file or library. If the *linker* can't find it, that's another matter. The lack of a *declaration* is a problem for ISO C - the behaviour is undefined under ISO C rules. But since the program was written before C was standardised, I don't suppose the author will be losing any sleep over that. -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Nov 20 '08 #27

 P: n/a Dik T. Winter said: In article Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Nov 20 '08 #28

 P: n/a In article , Keith Thompson As recently as gcc 4.3.2 on Ubuntu, "unix" *is* predefined as a macrowith the value 1. I've never heard of ti being an implicitly declaredautomatic variable. And here's an article referring to some commonly predefined macros in the mid-80s: http://groups.google.com/group/comp....hl=en&ie=UTF-8 -- Richard -- Please remember to mention me / in tapes you leave behind. Nov 20 '08 #29

 P: n/a Nate Eldredge Was it ever possible to implicitly declare *variables*? Only parameters: foo(i, j) { return i + j; } That implicitly declares i and j as int's. -- Larry Jones Yep, we'd probably be dead by now if it wasn't for Twinkies. -- Calvin Nov 20 '08 #30

 P: n/a Nate Eldredge (Language barrier: to me, these characters ( ) are parentheses. Braces are these { } . These [ ] are brackets.) Yes, in other dialects they're all brackets: round brackets, curly brackets, and square brackets, respectively. But I know of no dialect in which the round ones are called "braces" as the original poster did. Perhaps he was just using an unfortunate font. :-) -- Larry Jones Don't you hate it when your boogers freeze? -- Calvin Nov 20 '08 #31

 P: n/a la************@siemens.com wrote: >Was it ever possible to implicitly declare *variables*? Only parameters: foo(i, j) { return i + j; } That implicitly declares i and j as int's. That's not what Nate's talking about. "Implicitly" in this case means "without ever mentioning the variable's name in any declaration", as in foo(i) { return a + b + i; } This was never possible (at least formally). So the linked explanation is incorrect, although the idea with the storage for allegedly implicitly declared 'int unix' overlaying the system-provided 'argc' storage area had an undeniable perverted attractiveness. As for the declarations that omit everything besides the name, some older compiler would accept file-scope declarations without a storage class and type name. I don't remember whether it was intended to be legal in "C Reference Manual" times. -- Best regards, Andrey Tarasevich Nov 20 '08 #32

 P: n/a On November 20, 2008 12:26, in comp.lang.c, Andrey Tarasevich (an**************@hotmail.com) wrote: la************@siemens.com wrote: >>Was it ever possible to implicitly declare *variables*? [snip] As for the declarations that omit everything besides the name, some older compiler would accept file-scope declarations without a storage class and type name. I don't remember whether it was intended to be legal in "C Reference Manual" times. As I read it, the "C Reference Manual" requires that declarations have either a type specifier /or/ a storage class specifier /or/ both. But, you cannot omit both type specifier and storage class specifier. "8. Declarations ... Declarations have the form declaration: decl-specifiers declarator-list{opt} ; The declarators in the declarator list contain the identifiers being declared. The decl-specifiers consist of a sequence of type and storage class specifiers. decl-specifiers: type-specifier decl-specifiers{opt} sc-specifier decl-specifiers{opt} 8.1 Storage class specifiers The sc-specifiers are sc-specifier: auto static extern register typedef ... At most one sc-specifier may be given in a declaration. If the sc-specifier is missing from a declaration, it is taken to be auto inside a function, extern outside. Exception: functions are never automatic. 8.2 Type specifiers The type-specifiers are type-specifier: char short int long unsigned float double struct-or-union-specifier typedef-name ... If the type-specifier is missing from a declaration, it is taken to be int. " -- Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ Nov 20 '08 #33

 P: n/a c.***********@gmail.com wrote: 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... I get the following when attempting to compile this code: oneliner.c:1: warning: return type defaults to 'int' oneliner.c: In function 'main': oneliner.c:1: warning: implicit declaration of function 'printf' oneliner.c:1: warning: incompatible implicit declaration of built-in function 'printf' oneliner.c:1: error: 'unix' undeclared (first use in this function) oneliner.c:1: error: (Each undeclared identifier is reported only once oneliner.c:1: error: for each function it appears in.) oneliner.c:1: error: stray '\327' in program oneliner.c:1: error: expected ')' before numeric constant oneliner.c:1: warning: control reaches end of non-void function So much for output. ** Posted from http://www.teranews.com ** Nov 21 '08 #34

 P: n/a On Nov 20, 9:38 pm, Eric Schmidt /

 P: n/a Eric Schmidt 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... I get the following when attempting to compile this code: oneliner.c:1: warning: return type defaults to 'int' oneliner.c: In function 'main': oneliner.c:1: warning: implicit declaration of function 'printf' oneliner.c:1: warning: incompatible implicit declaration of built-in function 'printf' oneliner.c:1: error: 'unix' undeclared (first use in this function) oneliner.c:1: error: (Each undeclared identifier is reported only once oneliner.c:1: error: for each function it appears in.) oneliner.c:1: error: stray '\327' in program oneliner.c:1: error: expected ')' before numeric constant oneliner.c:1: warning: control reaches end of non-void function So much for output. The warnings are all about things that weren't necessarily considered problems in pre-ANSI C. The errors are caused by (a) the fact that you compiler doesn't pre-define "unix" as a macro, and (b) the original poster's use of a multiplication symbol rather than a letter 'x' in 0x60. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 21 '08 #36

 P: n/a William Hughes /

 P: n/a On Nov 21, 2:03 am, Nate Eldredge / I was thinking a.c: #error #include "a.c" would win with a ratio of zero. Nice. Perfectly within the spirit of GEB, too. - William Hughes Nov 21 '08 #38

### This discussion thread is closed

Replies have been disabled for this discussion.