There are a lot of reasons, some relatively trivial and some very serious. The trivial one is a nose bleed that drips down to the lungs.
In the old days it was very common to see people coughing up blood due to a disease called tuberculosis. This is less common now fortunately.
Other serious reasons include pneumonia, bronchitis, cancer and a range of lung diseases such as cystic fibrosis and pulomnary edema (swelling in the lung)
Comments