Let me add one more solution. **It is not really different from the accepted one, but it includes all details.** The problem is that a student without sufficient experience will not even see necessity to fill details.

**Theorem.**
*If $f\in C^\infty(\mathbb{R})$ and for every $x\in\mathbb{R}$ there is a nonnegative integer $n$ such that $f^{(n)}(x)=0$, then $f$ is a
polynomial.*

The following exercise shows that the result cannot be to easy.

**Exercise.** *Prove that there is a function $f\in C^{1000}(\mathbb{R})$ which is not a polynomial, but has the property described in the above
theorem.*

*Proof of the theorem.*
Let $\Omega\subset\mathbb{R}$ be the union of all open intervals $(a,b)\subset\mathbb{R}$
such that $f|_{(a,b)}$ is a polynomial. The set $\Omega$ is open, so
$$
\Omega=\bigcup_{i=1}^N (a_i, b_i)\, ,
\qquad \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1)
$$
where $a_i<b_i$ and $(a_i, b_i)\cap (a_j, b_j) = \emptyset$ for $i\neq j$, $1\leq N\leq\infty$.
Observe that $f|_{(a_i, b_i)}$ is a polynomial
(Why?)*.
We want to prove that $\Omega=\mathbb{R}$. First we will prove that
$\overline{\Omega}=\mathbb{R}$. To this end it suffices to prove that for any
interval $[a,b]$, $a<b$ we have $[a,b]\cap\Omega\neq\emptyset$.
Let
$$
E_n=\{x\in\mathbb{R}:\, f^{(n)}(x)=0\}\, .
$$
The sets $E_n\cap [a,b]$ are closed and
$$
[a,b]=\bigcup_{n=0}^\infty E_n\cap [a,b]\, .
$$
Since $[a,b]$ is complete, it follows from the Baire theorem that for some $n$
the set $E_n\cap [a,b]$ has nonempty interior (in the topology of $[a,b]$),
so there is $(c,d)\subset E_n\cap [a,b]$ such that $f^{(n)}=0$ on $(c,d)$.
Accordingly $f$ is a polynomial on $(c,d)$ and hence
$$
(c,d)\subset\Omega\cap [a,b]\neq\emptyset.
$$
The set $X=\mathbb{R}\setminus\Omega$ is closed and hence complete.
It remains to prove that $X=\emptyset$. Suppose not.
Observe that every point $x\in X$ is an accumulation point of the set,
i.e. there is a sequence $x_i\in X$, $x_i\neq x$, $x_i\to x$. Indeed,
otherwise $x$ would be an isolated point, i.e. there would be two intervals
$$
(a,x),\, (x,b)\subset\Omega,\ x\not\in\Omega\, .
\qquad \ \ \ \ \ \ \ \ \ \ \ \ (2)
$$
The function $f$ restricted to each of the two intervals is a polynomial, say
of degrees $n_1$ and $n_2$. If $n>\max\{ n_1, n_2\}$, then
$f^{(n)}=0$ on $(a,x)\cup (x,b)$. Since
$f^{(n)}$ is continuous on $(a,b)$, it must be zero on the entire interval
and hence $f$ is a polynomal of degree $\leq n-1$ on $(a,b)$, so
$(a,b)\subset\Omega$ which contradicts (2).

The space $X=\mathbb{R}\setminus\Omega$ is complete.
Since
$$
X=\bigcup_{n=1}^\infty X\cap E_n\, ,
$$
the second application of the Baire theorem
gives that $X\cap E_n$ has a nonempty interior in the topology of $X$, i.e.
there is an interval $(a,b)$ such that
$$
X\cap (a,b)\subset X\cap E_n\neq\emptyset\, .
\qquad \ \ \ \ \ \ \ \ \ \ \ \ \ (3)
$$
Accordingly $f^{(n)}(x)=0$ for all $x\in X\cap (a,b)$.
Since for every $x\in X\cap (a,b)$ there is a sequence $x_i\to x$, $x_i\neq x$
such that $f^{(n)}(x_i)=0$
it follows from the definition of the derivative that
$f^{(n+1)}(x)=0$ for every $x\in X\cap (a,b)$, and by induction
$f^{(m)}(x)=0$ for all $m\geq n$ and all $x\in X\cap (a,b)$.

We will prove that $f^{(n)}=0$ on $(a,b)$. This will imply that
$(a,b)\subset\Omega$ which is a contradiction with (3).
Since $f^{(n)}=0$ on $X\cap (a,b)=(a,b)\setminus\Omega$ it remains to prove that
$f^{(n)}=0$ on $(a,b)\cap\Omega$.
To this end it suffices to prove that for any interval
$(a_i, b_i)$ that appears in (1) such that
$(a_i, b_i)\cap (a,b)\neq\emptyset$, $f^{(n)}=0$ on $(a_i, b_i)$. Since
$(a,b)$ is not contained in $(a_i, b_i)$ one of the endpoints belongs
to $(a,b)$, say $a_i\in (a,b)$. Clearly
$a_i\in X\cap (a,b)$ and hence $f^{(m)}(a_i)=0$ for all $m\geq n$.
If $f$ is a polynomial of degree $k$ on $(a_i, b_i)$, then
$f^{(k)}$ is a nonzero constant on $(a_i, b_i)$, so $f^{(k)}(a_i)\neq 0$ by
continuity of the derivative. Thus $k<n$ and hence
$f^{(n)}=0$ on $(a_i,b_i)$.
$\Box$

**Exercise.**
*As the previous exercise shows the theorem is not true if we only assume that $f\in C^{1000}$. Where did we use in the proof the
assumption $f\in C^\infty(\mathbb{R})$?*

*It suffices to prove that $f$ is a polynomial on every compact
subinterval $[c,d]\subset (a_i, b_i)$. This subinterval has a finite covering by
open intervals
on which $f$ is a polynomial. Taking an integer $n$ larger than the maximum
of the degrees of these polynomials, we see that $f^{(n)}=0$ on $[c,d]$
and hence $f$ is a polynomial of degree $<n$ on $[c,d]$.