$C[a,b]$ is dense in $L^p([a,b])$ from the fact that $C_c(\mathbb{R}^n)$ is dense in $L^p(\mathbb{R}^n)$

We know that the following theorem holds.

Theorem. The space $$C_c(\mathbb{R}^n)$$ of the continuous functions with compact support is dense in $$L^p(\mathbb{R}^n)$$ for $$p\in [1,\infty).$$

I don't understand how from this theorem we can deduce that the space of continuous functions on $$[a,b]$$, $$C[a,b]$$ is dense in $$L^p([a,b])$$.

My reasoning is as follows: Let $$f\in C[a,b]$$, then $$\text{supp}(f)\subset [a,b]$$, since $$[a,b]$$ is bounded we have that $$\text{supp}(f)$$ is compact and then $$f\in C_c([a,b])$$, now I'm not sure if I can conclude with the above theorem. Could someone clarify my ideas?

• Can you approximate functions in $C([a,b])$ by functions in $C_c(\mathbb{R})$? (Hint: draw a picture). Commented Jul 6 at 10:57
• Note: $C_c([a,b])\subset C([a,b])$ Commented Jul 6 at 11:38
• @Thomas: $C([a,b])=C_c([a,b])$ Commented Jul 6 at 14:04

Let $$a,b \in \mathbb{R}$$ with $$a and $$p \in [1, \infty)$$. Consider the map $$\begin{split} J: L^p ([a,b]) &\longrightarrow L^p(\mathbb{R})\\ f &\longmapsto \bigg( x \mapsto \begin{cases} f(x), \ \text{if} \ x \in [a,b],\\ 0, \quad \text{else.} \end{cases} \bigg) \end{split}$$ Then $$J$$ is a linear isometry. Let $$f \in L^p([a,b])$$ and $$(f_n)_{n \in \mathbb{N}}$$ a sequence in $$C_c (\mathbb{R})$$ with $$f_n \to J(f)$$ as $$n \to \infty$$ in $$L^p(\mathbb{R})$$. Define the sequence $$(g_n)_{n \in \mathbb{N}}$$ in $$C([a,b])$$ by $$g_n:= f_n|_{[a,b]}$$. Let $$n \in \mathbb{N}$$. Then: $$\begin{split} \| f_n - J(f) \|^p_{L^p(\mathbb{R})} &= \int_{\mathbb{R}} |f_n(x) - (Jf)(x) |^p dx \\ &= \int_{[a,b]} |f_n(x) - f(x) |^p dx + \int_{\mathbb{R} \setminus [a,b]} |f_n(x)|^p dx \\ &\geq \int_{[a,b]} |f_n(x) - f(x) |^p dx = \| g_n - f\|^p_{L^p([a,b])} \geq 0. \end{split}$$ And therefore the sandwich lemma implies that $$g_n \to f$$ in $$L^p([a,b])$$ as $$n \to \infty$$.