Chemical abundances of 1111 FGK stars from the HARPS GTO planet search program. IV. Carbon and C/O ratios for Galactic stellar populations and planet hosts