Misplaced Pages

International Obfuscated C Code Contest

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

The International Obfuscated C Code Contest (abbreviated IOCCC ) is a computer programming contest for the most creatively obfuscated C code . Held semi-annually, it is described as "celebrating [C's] syntactical opaqueness". The winning code for the 27th contest, held in 2020, was released in July 2020. Previous contests were held in the years 1984–1996, 1998, 2000, 2001, 2004–2006, 2011–2015 and 2018–2020.

#811188

40-461: Entries are evaluated anonymously by a panel of judges. The judging process is documented in the competition guidelines and consists of elimination rounds. By tradition, no information is given about the total number of entries for each competition. Winning entries are awarded with a category, such as "Worst Abuse of the C preprocessor " or "Most Erratic Behavior", and then announced on the official IOCCC website. The contest states that being announced on

80-513: A 2011 entry which downsamples PGM , PPM images and ASCII art by Don, Yang: If the program is run using its own source as the input, the result is: C preprocessor The C preprocessor is the macro preprocessor for several computer programming languages , such as C , Objective-C , C++ , and a variety of Fortran languages. The preprocessor provides inclusion of header files , macro expansions, conditional compilation , and line control. The language of preprocessor directives

120-399: A different set of include files can be swapped in for different operating systems, for instance. By convention, include files are named with either a .h or .hpp extension. However, there is no requirement that this be observed. Files with a .def extension may denote files designed to be included multiple times, each time expanding the same repetitive content; #include "icon.xbm"

160-437: A function-like macro is: This defines a radians -to-degrees conversion which can be inserted in the code where required; for example, RADTODEG(34) . This is expanded in-place, so that repeated multiplication by the constant is not shown throughout the code. The macro here is written as all uppercase to emphasize that it is a macro, not a compiled function. The second x is enclosed in its own pair of parentheses to avoid

200-471: A pointer to the number is required, is to apply the const qualifier to a global variable. This causes the value to be stored in memory, instead of being substituted by the preprocessor. However, in modern C++ code, the constexpr keyword, introduced in C++11 , is used instead: Usages of variables declared as constexpr , as object-like macros, may be replaced with their value at compile-time. An example of

240-418: A set of guidelines that attempt to convey the spirit of the rules. Hacking the contest rules is a tradition. — Landon Curt Noll, 2011 The rules are often deliberately written with loopholes that contestants are encouraged to find and abuse. Entries that take advantage of loopholes can cause the rules for the following year's contest to be adjusted. Entries often employ strange or unusual tricks, such as using

280-407: A variable number of parameters, such as printf , for example when logging warnings and errors. One little-known usage pattern of the C preprocessor is known as X-Macros . An X-Macro is a header file . Commonly, these use the extension .def instead of the traditional .h . This file contains a list of similar macro calls, which can be referred to as "component macros." The include file

320-435: Is also a left parenthesis that begins the argument list of the macro invocation. The exact procedure followed for expansion of function-like macros with arguments is subtle. Object-like macros were conventionally used as part of good programming practice to create symbolic names for constants; for example: instead of hard-coding numbers throughout the code. An alternative in both C and C++, especially in situations in which

360-505: Is available. Because Toledo Nanochess is based on Toledo's winning entry from the 18th IOCCC (Best Game), it is heavily obfuscated . On February 2, 2014, the author published the book Toledo Nanochess: The commented source code , which contains the fully commented source code. As of February 7, 2010, it appears to be one of only two chess engines written in less than 2 kilobytes of C that are able to play full legal chess moves, along with Micro-Max by Dutch physicist H. G. Muller. In 2014

400-514: Is described by official standards for these languages, such as the ISO C standard. Implementations may provide their own extensions and deviations, and vary in their degree of compliance with written standards. Their exact behavior may depend on command-line flags supplied on invocation. For instance, the GNU C preprocessor can be made more standards compliant by supplying certain flags. The #pragma directive

440-642: Is initialized using an #embed directive, the result is the same as-if the resource was written to the array using fread (unless a parameter changes the embed element width to something other than CHAR_BIT ). Apart from the convenience, #embed is also easier for compilers to handle, since they are allowed to skip expanding the directive to its full form due to the as-if rule . The file to be embedded can be specified in an identical fashion to #include , meaning, either between chevrons or between quotes. The directive also allows certain parameters to be passed to it to customise its behaviour, which follow

SECTION 10

#1732852583812

480-709: Is likely to refer to an XBM image file (which is at the same time a C source file). #include often compels the use of #include guards or #pragma once to prevent double inclusion. The if–else directives #if , #ifdef , #ifndef , #else , #elif , and #endif can be used for conditional compilation . #ifdef and #ifndef are simple shorthands for #if defined(...) and #if !defined(...) . Most compilers targeting Microsoft Windows implicitly define _WIN32 . This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define WIN32 instead. For such compilers that do not implicitly define

520-455: Is not empty. Finally, the if_empty parameter replaces the entire directive if the resource is empty (which happens if the file is empty or a limit of 0 is specified). All standard parameters can also be surrounded by double underscores, just like standard attributes on C23, for example __prefix__ is interchangeable with prefix . Implementation-defined parameters use a form similar to attribute syntax (e.g., vendor::attr ) but without

560-501: Is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files . The preprocessor was introduced to C around 1973 at the urging of Alan Snyder and also in recognition of the usefulness of the file inclusion mechanisms available in BCPL and PL/I . Its original version offered only file inclusion and simple string replacement using #include and #define for parameterless macros, respectively. It

600-408: Is present, the macro will be interpreted as object-like with everything starting from the first parenthesis added to the token list. A macro definition can be removed with #undef : Whenever the identifier appears in the source code it is replaced with the replacement token list, which can be empty. For an identifier declared to be a function-like macro, it is only replaced when the following token

640-468: Is the following flight simulator, the winner of the 1998 IOCCC, as listed and described in Calculated Bets: Computers, Gambling, and Mathematical Modeling to Win (2001) and shown below: This program needs the following command line on a Linux system to be compiled: In order to run the binary file ( banks ) it has to be supplied with a .sc scenery file via stdin input: Below is

680-520: Is then referenced repeatedly. Many compilers define additional, non-standard macros, although these are often poorly documented. A common reference for these macros is the Pre-defined C/C++ Compiler Macros project , which lists "various pre-defined compiler macros that can be used to identify standards, compilers, operating systems, hardware architectures, and even basic run-time libraries at compile-time." The # operator (known as

720-440: The #error directive: There are two types of macros: object-like and function-like . Object-like macros do not take parameters; function-like macros do (although the list of parameters may be empty). The generic syntax for declaring an identifier as a macro of each type is, respectively: The function-like macro declaration must not have any whitespace between the identifier and the first, opening parenthesis. If whitespace

760-448: The _WIN32 macro, it can be specified on the compiler's command line, using -D_WIN32 . The example code tests if a macro __unix__ is defined. If it is, the file <unistd.h> is then included. Otherwise, it tests if a macro _WIN32 is defined instead. If it is, the file <windows.h> is then included. A more complex #if example can use operators; for example: Translation can also be caused to fail by using

800-402: The C preprocessor to do things it was not designed to do (in some cases "spectacularly", according to Dr. Dobbs , with one entry creating an 11-bit ALU in the C preprocessor), or avoiding commonly used constructs in the C programming language in favor of much more obscure ways of achieving the same thing. Contributions have included source code formatted to resemble images, text, etc., after

840-437: The stringification operator or stringizing operator ) converts a token into a C string literal , escaping any quotes or backslashes appropriately. Example: If stringification of the expansion of a macro argument is desired, two levels of macros must be used: A macro argument cannot be combined with additional text and then stringified. However, a series of adjacent string constants and stringified arguments can be written:

SECTION 20

#1732852583812

880-630: The 1 kilobyte barrier was broken by Super Micro Chess – a derivative of Micro-Max – totaling 760 characters (spaces and newlines included). There is also a smaller version of Toledo's engine, the Toledo Picochess , consisting of 944 non-blank characters. Source code excerpt Below is a 1988 entry which calculates pi by looking at its own area : (This entry was written in K&;R C ; it does not work correctly in ANSI C without some changes.) Another example

920-411: The C compiler will then combine all the adjacent string constants into one long string. The ## operator (known as the "Token Pasting Operator") concatenates two tokens into one token. Example: The #error directive outputs a message through the error stream. C23 will introduce the #embed directive for binary resource inclusion. This allows binary files (like images) to be included into

960-536: The IOCCC website is the reward for winning. The IOCCC was started by Landon Curt Noll and Larry Bassel in 1984 while employed at National Semiconductor's Genix porting group. The idea for the contest came after they compared notes with each other about some poorly written code that they had to fix, notably the Bourne shell , which used macros to emulate ALGOL 68 syntax, and a buggy version of finger for BSD. The contest itself

1000-475: The code size limit of only a few kilobytes, contestants have managed to do complicated things – a 2004 winner turned out an operating system. Toledo Nanochess is a chess engine created by Mexican software developer Oscar Toledo Gutiérrez, a five-time winner of the IOCCC. In accordance with IOCCC rules, it is 1255 characters long. The author claims that it is the world's smallest chess program written in C. The source code for Toledo Nanochess and other engines

1040-412: The division. Function-like macro expansion occurs in the following stages: This may produce surprising results: Certain symbols are required to be defined by an implementation during preprocessing. These include __FILE__ and __LINE__ , predefined by the preprocessor itself, which expand into the current file and line number. For instance, the following: prints the value of x , preceded by

1080-511: The file and line number to the error stream, allowing quick access to which line the message was produced on. Note that the WHERESTR argument is concatenated with the string following it. The values of __FILE__ and __LINE__ can be manipulated with the #line directive. The #line directive determines the line number and the file name of the line below. For example: generates the printf function: Source code debuggers refer also to

1120-414: The file name. The C standard defines the following parameters and implementations may define their own. The limit parameter is used to limit the width of the included data. It is mostly intended to be used with "infinite" files like urandom . The prefix and suffix parameters allow the programmer to specify a prefix and suffix to the embedded data, which is used if and only if the embedded resource

1160-419: The manner of ASCII art , preprocessor redefinitions to make code harder to read, and self-modifying code . In several years an entry was submitted that required a new definition of some of the rules for the next year, regarded as a high honor. An example is the world's shortest self-reproducing program . The entry was a program designed to output its own source code, and which had zero bytes of source code. When

1200-408: The most common uses of the preprocessor is to include another source file: The preprocessor replaces the line #include <stdio.h> with the textual content of the file 'stdio.h', which declares the printf() function among other things. This can also be written using double quotes, e.g. #include "stdio.h" . If the filename is enclosed within angle brackets, the file is searched for in

1240-552: The name of the function definition within which it is contained, but because the preprocessor is agnostic to the grammar of C, this must be done in the compiler itself using a variable local to the function. Macros that can take a varying number of arguments ( variadic macros ) are not allowed in C89, but were introduced by a number of compilers and standardized in C99 . Variadic macros are particularly useful when writing wrappers to functions taking

International Obfuscated C Code Contest - Misplaced Pages Continue

1280-553: The possibility of incorrect order of operations when it is an expression instead of a single value. For example, the expression RADTODEG ( r + 1 ) expands correctly as (( r + 1 ) * 57.29578 ) ; without parentheses, ( r + 1 * 57.29578 ) gives precedence to the multiplication. Similarly, the outer pair of parentheses maintain correct order of operation. For example, 1 / RADTODEG ( r ) expands to 1 / (( r ) * 57.29578 ) ; without parentheses, 1 / ( r ) * 57.29578 gives precedence to

1320-419: The program ran, it printed out zero bytes, equivalent to its source code. In the effort to take obfuscation to its extremes, contestants have produced programs which skirt around the edges of C standards, or result in constructs which trigger rarely used code path combinations in compilers. As a result, several of the past entries may not compile directly in a modern compiler, and some may cause crashes. Within

1360-398: The program without them being valid C source files (like XBM), without requiring processing by external tools like xxd -i and without the use of string literals which have a length limit on MSVC . Similarly to xxd -i the directive is replaced by a comma separated list of integers corresponding to the data of the specified resource. More precisely, if an array of type unsigned char

1400-459: The source position defined with __FILE__ and __LINE__ . This allows source code debugging when C is used as the target language of a compiler, for a totally different language. The first C Standard specified that the macro __STDC__ be defined to 1 if the implementation conforms to the ISO Standard and 0 otherwise, and the macro __STDC_VERSION__ defined as a numeric literal specifying

1440-441: The square brackets. While all standard parameters require an argument to be passed to them (e.g., limit requires a width), this is generally optional and even the set of parentheses can be omitted if an argument is not required, which might be the case for some implementation-defined parameters. All C, C++, and Objective-C implementations provide a preprocessor, as preprocessing is a required step for those languages, and its behavior

1480-399: The standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source file directory. C compilers and programming environments all have a facility that allows the programmer to define where include files can be found. This can be introduced through a command-line flag, which can be parameterized using a makefile , so that

1520-477: The version of the Standard supported by the implementation. Standard C++ compilers support the __cplusplus macro. Compilers running in non-standard mode must not set these macros or must define others to signal the differences. Other Standard macros include __DATE__ , the current date, and __TIME__ , the current time. The second edition of the C Standard, C99 , added support for __func__ , which contains

1560-409: Was extended shortly after, firstly by Mike Lesk and then by John Reiser, to incorporate macros with arguments and conditional compilation. The C preprocessor was part of a long macro-language tradition at Bell Labs, which was started by Douglas Eastwood and Douglas McIlroy in 1959. Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard. One of

1600-488: Was the topic of a quiz question in the 1993 Computer Bowl. After a hiatus of five years starting in 2006, the contest returned in 2011. Compared with other programming contests, the IOCCC is described as "not all that serious" by Michael Swaine , editor of Dr. Dobb's Journal . Each year, the rules of the contest are published on the IOCCC website. All material is published under Creative Commons license BY-SA 3.0 Unported. Rules vary from year to year and are posted with

#811188