We define a generalization of the winding number of a piecewise C1 cycle in the complex plane which has a geometric meaning also for points which lie on the cycle. The computation of this winding number relies on the Cauchy principal value but is also possible in a real version via an integral with bounded integrand. The new winding number allows to establish a generalized residue theorem which covers also the situation where singularities lie on the cycle. This residue theorem can be used to calculate the value of improper integrals for which the standard technique with the classical residue theorem does not apply.