The solid inner core of the Earth is predominantly composed of iron alloyed with several percent Ni and some lighter elements, Si, S, O, H, and C being the prime candidates. There have been a growing number of papers investigating C and H as possible light elements in the core, but the results are contradictory. Here, using ab initio simulations, we study the Fe-C and Fe-H systems at inner core pressures (330-364 GPa). Using the evolutionary structure prediction algorithm USPEX, we have determined the lowest-enthalpy structures of possible carbides (FeC, Fe2C, Fe3C, Fe4C, FeC2, FeC3, FeC4 and Fe7C3) and hydrides (Fe4H, Fe3H, Fe2H, FeH, FeH2, FeH3, FeH4) and have found that Fe2C (Pnma) is the most stable iron carbide at pressures of the inner core, while FeH, FeH3 and FeH4 are stable iron hydrides at these conditions. For Fe3C, the cementite structure (Pnma) and the Cmcm structure recently found by random sampling are less stable than the I-4 and C2/m structures found here. We found that FeH3 and FeH4 adopt chemically interesting thermodynamically stable structures, in both compounds containing trivalent iron. The density of the inner core can be matched with a reasonable concentration of carbon, 11-15 mol.percent (2.6-3.7 wt.percent) at relevant pressures and temperatures. This concentration matches that in CI carbonaceous chondrites and corresponds to the average atomic mass in the range 49.3-51.0, in close agreement with inferences from the Birchs law for the inner core. Similarly made estimates for the maximum hydrogen content are unrealistically high, 17-22 mol.percent (0.4-0.5 wt.percent), which corresponds to the average atomic mass in the range 43.8-46.5. We conclude that carbon is a better candidate light alloying element than hydrogen.