Context. Direct observations of gaseous exoplanets reveal that their gas envelope has a higher C/O ratio than that of the host star (e.g., . This has been explained by considering that the gas phase of the disc could be inhomogeneous, exceeding the stellar C/O ratio in regions where these planets formed; but few studies have considered the drift of the gas and planet migration. Aims. We aim to derive the gas composition in planets through planet formation to evaluate if the formation of giant planets with an enriched C/O ratio is possible. The study focusses on the effects of different processes on the C/O ratio, such as the disc evolution, the drift of gas, and planet migration. Methods. We used our previous models for computing the chemical composition, together with a planet formation model, to which we added the composition and drift of the gas phase of the disc, which is composed of the main volatile species H 2 O, CO, CO 2 , NH 3 , N 2 , CH 3 OH, CH 4 , and H 2 S, H 2 and He. The study focusses on the region where ice lines are present and influence the C/O ratio of the planets. Results. Modelling shows that the condensation of volatile species as a function of radial distance allows for C/O enrichment in specific parts of the protoplanetary disc of up to four times the solar value. This leads to the formation of planets that can be enriched in C/O in their envelope up to three times the solar value. Planet migration, gas phase evolution and disc irradiation enables the evolution of the initial C/O ratio that decreases in the outer part of the disc and increases in the inner part of the disc. The total C/O ratio of the planets is governed by the contribution of ices accreted, suggesting that high C/O ratios measured in planetary atmospheres are indicative of a lack of exchange of material between the core of a planet and its envelope or an observational bias. It also suggests that the observed C/O ratio is not representative of the total C/O ratio of the planet.