Magic number (programming)

This has been referred to as breaking one of the oldest rules of programming, dating back to the COBOL, FORTRAN and PL/1 manuals of the 1960s.

[4] Replacing all significant magic numbers with named constants (also called explanatory variables) makes programs easier to read, understand and maintain.

It is considered better programming style to write the following: This is preferable for several reasons: Disadvantages are: In some contexts, the use of unnamed numerical constants is generally accepted (and arguably "not magic").

With the development of paged versions of Unix, a header was created to describe the executable image components.

[8] In the Sixth Edition source code of the Unix program loader, the exec() function read the executable (binary) image from the file system.

The first 8 bytes of the file was a header containing the sizes of the program (text) and initialized (global) data areas.

Since the Sixth and Seventh Editions of Unix employed paging code, the dual role of the header constant was hidden.

In Version Seven Unix, the header constant was not tested directly, but assigned to a variable labeled ux_mag[10] and subsequently referred to as the magic number.

Magic numbers are common in API functions and interfaces across many operating systems, including DOS, Windows and NetWare: This is a list of limits of data storage types:[16] It is possible to create or alter globally unique identifiers (GUIDs) so that they are memorable, but this is highly discouraged as it compromises their strength as near-unique identifiers.

[17][18] The specifications for generating GUIDs and UUIDs are quite complex, which is what leads to them being virtually unique, if properly implemented.

Since it is very unlikely, although possible, that a 32-bit integer would take this specific value, the appearance of such a number in a debugger or memory dump most likely indicates an error such as a buffer overflow or an uninitialized variable.

Some IP cameras expect the player to send this magic number and do not start the stream if it is not received.