BACKGROUND The effectiveness of prophylactic continuous positive pressure ventilation (CPAP) after thoracic surgery is not clearly established. OBJECTIVE The aim of this study was to assess the effectiveness of CPAP immediately after lung resection either by thoracotomy or thoracoscopy in preventing atelectasis and pneumonia. DESIGN A multicentre, randomised, controlled, open-label trial. SETTINGS Four large University hospitals at Madrid (Spain) from March 2014 to December 2016. PATIENTS Immunocompetent patients scheduled for lung resection, without previous diagnosis of sleep-apnoea syndrome or severe bullous emphysema. Four hundred and sixty-four patients were assessed, 426 were randomised and 422 were finally analysed. INTERVENTION Six hours of continuous CPAP through a Boussignac system versus standard care. MAIN OUTCOME MEASURES Primary outcome: incidence of the composite endpoint ‘atelectasis + pneumonia’. Secondary outcome: incidence of the composite endpoint ‘persistent air leak + pneumothorax’. RESULTS The primary outcome occurred in 35 patients (17%) of the CPAP group and in 58 (27%) of the control group [adjusted relative risk (ARR) 0.53, 95% CI 0.30 to 0.93]. The secondary outcome occurred in 33 patients (16%) of the CPAP group and in 29 (14%) of the control group [ARR 0.92, 95% CI 0.51 to 1.65]. CONCLUSION Prophylactic CPAP decreased the incidence of the composite endpoint ‘postoperative atelectasis + pneumonia’ without increasing the incidence of the endpoint ‘postoperative persistent air leaks + pneumothorax’.