Dimension of the $0^{\text{th}}$ de Rham cohomology group of $U$ Let $U\subset\mathbb{R}^n$ be an open set. I proved that 
$$
H^0_{DR}(U):=\frac{\text{closed forms}}{\text{exact forms}}=\{f\in C^{\infty}(U):\,f\,\,\text{is locally constant} \}
$$
I have to show that $\dim H^0_{DR}=\text{number of connected components of}\,\, U$.
Here is my incomplete proof.
Let's write $U=\bigcup_i U_i$ where $U_i$ are the connected components of $U$.
I have to prove that $f$ is constant on each $U_i$. How?
Then it is sufficient to consider the set
$$
\mathcal{B}=\{\chi_{U_i}\}_i
$$
which is a basis for $H^0_{DR}(U)$.
 A: A function $f : U \to \mathbb{R}$ is locally constant if for each $x \in U$, there is an open neighbourhood $V$ of $x$ such that $f|_V$ is constant. 
Note that for any $y \in \mathbb{R}$, $f^{-1}(y)$ is open, so for any $A\subseteq \mathbb{R}$, $f^{-1}(A) = \bigcup_{y\in A}f^{-1}(y)$ is open. In particular, $f^{-1}(A)$ is open for every open $A\subseteq \mathbb{R}$ so locally constant functions are continuous. 
By continuity, $f^{-1}(y)$ is closed. 
Now suppose that $U$ is connected. If $y \in f(U)$, then $f^{-1}(y)$ is non-empty and by the above it is both open and closed. As $U$ is connected, we have $f^{-1}(y) = U$. That is, $f$ is the constant function with value $y$.
If $U$ is not connected, let $U_i$ be a connected component of $U$ and then the above argument shows that $f|_{U_i}$ is constant.
A: Hint: Suppose $X$ is a non-empty connected open set, and that $f:X \to \mathbf{R}$ is locally constant. Pick an arbitrary point $x_{0}$ in $X$, and let $U = \{x \in X : f(x) = f(x_{0})\}$.
Use local constancy of $f$ to prove $U$ is open and closed in $X$.
