4. The sequence $\frac{1}{2}, \frac{1}{3}, \frac{2}{3}, \frac{1}{4}, \frac{2}{4}, \frac{3}{4}, \cdots$, $\frac{1}{m+1}, \frac{2}{m+1}, \cdots, \frac{m}{m+1}, \cdots$ has the sum of its first 40 terms as ( ).
(A) $23 \frac{1}{2}$
(B) $19 \frac{1}{9}$
(C) 19
(D) 18
4. C.

Let the denominator of the 40th term $a_{40}$ be $m+1$. Then
$$
\frac{m(m-1)}{2}<40 \leqslant \frac{m(m+1)}{2} \text {. }
$$

Solving this, we get $m=9 \Rightarrow \frac{m(m-1)}{2}=36$.
Thus, $a_{40}=\frac{4}{10}$,
$$
\begin{array}{l}
S_{40}=\sum_{k=1}^{8} \sum_{i=1}^{k} \frac{i}{k+1}+\frac{1}{10}(1+2+3+4) \\
=\sum_{k=1}^{8} \frac{k}{2}+1=19 .
\end{array}
$$