The early universe contained only the lightest elements: hydrogen and helium. Heavier elements, such as carbon, were produced only when stars underwent nuclear reactions and emitted when stars exploded, and existed in subsequent gas clouds. Recently, it was found that a gas cloud contained carbon 4 billion years ago, when there were only stars less than 2 billion years old.
The carbon in the gas cloud then forms part of the star can be inferred.