The main source of hydrocarbons in a gasoline engine is the fuel/air mixture right next to all of the metal surfaces inside the engine. In a modern engine, combustion in the chamber is nearly complete, but as the flame approaches any metal surface (the cylinder head, valves, combustion chamber walls, and piston top; it gets quenched. Therefore, at the end of every cycle, there is a thin layer of unburned fuel (hydrocarbons) that gets exhausted with the products of combustion. In addition, any crevices in the chamber, like right above the top piston ring, next to the valve seats, etc.) don’t let the flame in and this adds to the unburned fuel. On the average this totals up to 2–4% of the fuel.
Diesel engines are quite different, because the cylinder does not get filled with a fuel air mixture. A modern Diesel engine will have very low HC emissions