The nature of light element(s) in the core holds key to our understanding of Earth's history of accretion and differentiation, but the core composition remains poorly constrained. Carbon has been proposed to be a major constituent of the inner core, with broad implications for the global carbon cycle, the budget of volatiles in the Earth and origin of carbon-based life in the Solar System. However, existing estimates of the inner core's carbon content remain highly controversial because of poor constraints on the behavior of compressed iron carbides. Here we investigated the structure, elasticity, and magnetism of Eckstrom-Adcock carbide Fe7C3up to core pressures, using synchrotron-based single-crystal X-ray diffraction and Mössbauer spectroscopy techniques. We detected two discontinuities in the compression curve up to 167 gigapascals (GPa), the first of which corresponds to a magnetic collapse between 5.5 and 7.5 GPa and is attributed to a ferromagnetic to paramagnetic transition. At the second discontinuity near 53 GPa, Fe7C3softens and exhibits Invar behavior, presumably caused by a high-spin to low-spin transition. Considering the magneto-elastic coupling effects, an Fe7C3-dominant composition can match the density of the inner core, making the core potentially the largest reservoir of carbon in Earth.