Carbon (C) is one of the candidate light elements proposed to account for the density deficit of the Earth's core. In addition, C significantly affects siderophile and chalcophile element partitioning between metal and silicate and thus the distribution of these elements in the Earth's core and mantle. Derivation of the accretion and core-mantle segregation history of the Earth requires, therefore, an accurate knowledge of the C abundance in the Earth's core. Previous estimates of the C content of the core differ by a factor of ∼20 due to differences in assumptions and methods, and because the metal-silicate partition coefficient of C was previously unknown. Here we use two-phase first-principles molecular dynamics to derive this partition coefficient of C between liquid iron and silicate melt. We calculate a value of 9 ± 3 at 3,200 K and 40 GPa. Using this partition coefficient and the most recent estimates of bulk Earth or mantle C contents, we infer that the Earth's core contains 0.1-0.7 wt% of C. Carbon thus plays a moderate role in the density deficit of the core and in the distribution of siderophile and chalcophile elements during core-mantle segregation processes. The partition coefficients of nitrogen (N), hydrogen, helium, phosphorus, magnesium, oxygen, and silicon are also inferred and found to be in close agreement with experiments and other geochemical constraints. Contents of these elements in the core derived from applying these partition coefficients match those derived by using the cosmochemical volatility curve and geochemical mass balance arguments. N is an exception, indicating its retention in a mantle phase instead of in the core.