The word 'DEM' is also used to indicate DEMs in general, of which there are many formats (analagous to how 'images' or 'audio' have many formats). Examples include BIL, GeoTIFF, XYZ, STM, StL, NTF, GTOPO30, etc. Some image formats also have "repurposing" interpretations to treat them as raster elevation data, such as the POV-Ray heightfield red/green RGB channel interpretation. Treating grayscale image bitmaps as heightfields is even more common. "USGS DEM", however, tends to mean a particular format with a precisely specified structure, as described in the USGS document "Standards for Digital Elevation Models"
'''Format Structure
The USGS DEM format is a self-contained (single file) set of ASCII-encoded (text) 1024-byte blocks that fall into three record categories called A, B, and C. There is no cross-platform ambiguity since line ending control codes are not used, and all data including numbers is represented in readable text form. There is no known binary analogue of the format, although it is common practice to compress the files with gzip.
Floating-point numbers are encoded using FORTRAN scientific notation, so C/C++ programs need to swap the "D" exponent-indicating character with "E" when parsing (and vice versa when writing).
The A record appears once as the file's header, the C record also appears once as the trailer, and multiple B records (called profiles) comprise the elevation data. A and C records each fit within one block but a single B record typically requires multiple blocks. When such block-spanning occurs, data is shifted to start cleanly on each block boundary. A records also come in "old" and "new" flavors, because the USGS added several fields to the A record.
The fields in the A record hold the origin, type, summary statistics and the measurement systems used by the profiles. One of the key items is the quadrangle, which is a set of four terrestrial coordinates describing the four-sided polygon enclosing the area of interest.
B records (profiles) are a variable-length longitudonal column of raster elevations that start at a specified location. They are some multiple of 1024 bytes long and contain a small header summarizing the profile. The elevations are contiguous; breaks or other discontinuities are expressed using "void" elevations of value -32767. Each elevation is described as a six-character readable integer occupying a fixed location in a block. The profile header only appears in the first block, so subsequent blocks hold more elevation values. When reading the DEM file from first byte to last, one reads the profiles as columns from west to east. The elevations within a profile run from south to north.
The variable-location and variable-length nature of profiles stems mainly from the use of the UTM (Universal Tranverse Mercator) ground reference system. Since measurements within UTM employ fixed distances (e.g., 30 meters between elevation samples), the quadrangle must slightly distort to map such locations onto the spherical Earth. This distortion usually manifests as a rotated square, hence the elevation columns near the east and west edges start more northward and contain fewer samples.
The C record contains root-mean squared error (RMSE) quality control data, using ten six-character integer fields.