Magic number (programming)

magic numbermagic numbersMagic debug values0xDEADBEEFCAFEBABEmagicmagic ASCII stringmagic constantmagic debug valuemagic string
In computer programming, the term magic number has multiple meanings.wikipedia
271 Related Articles

List of file signatures

file signaturesignature or ''magic'' number
Such signatures are also known as magic numbers or Magic Bytes.

Test-driven development

Test Driven Developmenttest-first developmentTDD
Thus, declaring is better than several occurrences of the 'magic value' in a test suite.


animated GIF.gifGraphics Interchange Format
The two versions can be distinguished by looking at the first six bytes of the file (the "magic number" or signature), which, when interpreted as ASCII, read "GIF87a" and "GIF89a", respectively.


The term magic number or magic constant refers to the anti-pattern of using numbers directly in source code.

DOS MZ executable

MZDOS executableDOS .EXE files
The file can be identified by the ASCII string "MZ" (hexadecimal: 4D 5A) at the beginning of the file (the "magic number").

Byte order mark

Microsoft compilers and interpreters, and many pieces of software on Microsoft Windows such as Notepad treat the BOM as a required magic number rather than use heuristics.

Java class file

Java classclass fileclass files
The history of this magic number was explained by James Gosling referring to a restaurant in Palo Alto:

Interpreter directive

In Unix, Linux and other Unix-like operating systems, the first two bytes in a file can be the characters "#!", which constitute a magic number (hexadecimal 23 and 21, the ASCII values of "#" and "!") often referred to as shebang, prefix the first line in a script, with the remainder of the line being a command usually limited to a max of 14 (when introduced) up to usually about 80 characters in 2016.

File format

formatfile formatsformats
Detecting such constants in files is a simple and effective way of distinguishing between many file formats and can yield further run-time information.


Hexadecimal "words
Memory is usually viewed in hexadecimal, so memorable repeating or hexspeak values are common.
Created by programmers as memorable magic numbers, hexspeak words can serve as a clear and unique identifier with which to mark memory or data.

In-band signaling

in-bandin-band signallinginband
Magic numbers implement strongly typed data and are a form of in-band signaling to the controlling program that reads the data type(s) at program run-time.
In computer programming, an example of in-band signaling are magic numbers, used for signaling of file formats.

Shebang (Unix)

shebanghashbangshebang line
The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being 0x23 0x21, the two-character encoding in ASCII of #!.

Magic (programming)

magicDeep magicblack magic
Thus, the header constant did provide an illusion and met the criteria for magic.

Marshall Kirk McKusick

Kirk McKusickM. Kirk McKusickM. McKusick
The magic number used in the UFS2 super block structure reflects McKusick's birth date: #define FS_UFS2_MAGIC 0x19540119 (as found in /usr/include/ufs/ffs/fs.h on FreeBSD systems).


Amiga OSAmigaWorkbench
Executable programs are recognized using a magic number.


Such a value may also be used as a sentinel value to initialize newly allocated memory for debugging purposes.

Enumerated type

These arbitrary values were sometimes referred to as magic numbers since there often was no explanation as to how the numbers were obtained or whether their actual values were significant.

Fast inverse square root

The purpose of the constant is not immediately clear to someone viewing the code, so, like other such constants found in code, it is often called a magic number.

Hard coding

hard-codedhard codedhardcoded
This would be a magic number.