[proofplan]
We prove boundedness and attainment separately. For boundedness, we cover $X$ by preimages of bounded intervals and extract a finite subcover. For attainment, we suppose the supremum $M$ is not attained, construct a nested open cover from the sets $f^{-1}((-\infty, M - 1/n))$, and use compactness to derive a contradiction: the finite subcover forces $f$ to be bounded strictly below $M$. The infimum case follows by applying the supremum result to $-f$.
[/proofplan]
[step:Prove $f$ is bounded by extracting a finite subcover from preimages of bounded intervals]
For each $n \in \mathbb{N}$, define $U_n = f^{-1}((-n, n))$. Since $f$ is [continuous](/page/Continuity) and $(-n, n)$ is open in $\mathbb{R}$, each $U_n$ is open in $X$. For every $x \in X$, $f(x) \in \mathbb{R}$, so $|f(x)| < n$ for $n$ sufficiently large, giving $x \in U_n$. Therefore $\{U_n\}_{n \in \mathbb{N}}$ is an open cover of $X$.
By compactness of $X$, there exist $n_1 < n_2 < \cdots < n_k$ with $X = U_{n_1} \cup \cdots \cup U_{n_k}$. Since the sets are nested ($U_{n_1} \subseteq U_{n_2} \subseteq \cdots$), we have $X = U_{n_k} = f^{-1}((-n_k, n_k))$, so $f(X) \subseteq (-n_k, n_k)$. In particular, $f$ is bounded.
[/step]
[step:Show $f$ attains its supremum by contradiction using compactness]
Let $M = \sup_{x \in X} f(x)$, which is finite by boundedness. Suppose for contradiction that $M \notin f(X)$, i.e., $f(x) < M$ for every $x \in X$. For each $n \in \mathbb{N}$, define
\begin{align*}
V_n = f^{-1}\!\left((-\infty, M - 1/n)\right).
\end{align*}
Each $V_n$ is open in $X$ (as the preimage of an [open set](/page/Open%20Set) under a continuous function). Since $f(x) < M$ for every $x \in X$, there exists $n$ with $f(x) < M - 1/n$ (because $f(x) < M$ means $M - f(x) > 0$, so choose $n > 1/(M - f(x))$). Therefore $\{V_n\}_{n \in \mathbb{N}}$ is an open cover of $X$.
By compactness, there exist $n_1 < \cdots < n_k$ with $X = V_{n_1} \cup \cdots \cup V_{n_k}$. The sets are nested ($V_1 \subseteq V_2 \subseteq \cdots$), so $X = V_{n_k}$, meaning $f(x) < M - 1/n_k$ for all $x \in X$. This gives $\sup_{x \in X} f(x) \leq M - 1/n_k < M$, contradicting $M = \sup_{x \in X} f(x)$.
Therefore $M \in f(X)$: there exists $x_{\max} \in X$ with $f(x_{\max}) = M = \sup_{x \in X} f(x)$.
[guided]
We want to show $M = \sup_{x \in X} f(x)$ is attained. Assume for contradiction that $f(x) < M$ for every $x \in X$.
For each $x$, the gap $M - f(x) > 0$ varies from point to point. Define $V_n = f^{-1}((-\infty, M - 1/n))$. Each $V_n$ is open (preimage of an open set under a continuous function). The sets form a nested increasing sequence: $V_1 \subseteq V_2 \subseteq \cdots$.
Their union covers $X$: for any $x \in X$, the assumption $f(x) < M$ gives $M - f(x) > 0$, so choosing $n > 1/(M - f(x))$ places $x \in V_n$. Therefore $\{V_n\}_{n \in \mathbb{N}}$ is an open cover of $X$.
Compactness of $X$ gives a finite subcover. Since the sets are nested, the finite subcover reduces to a single set $V_{n_k}$ (the one with the largest index). This yields $X = V_{n_k}$, meaning $f(x) < M - 1/n_k$ for all $x \in X$.
But then $\sup_{x \in X} f(x) \leq M - 1/n_k < M$, contradicting $M = \sup_{x \in X} f(x)$. The contradiction proves there exists $x_{\max}$ with $f(x_{\max}) = M$.
[/guided]
[/step]
[step:Deduce attainment of the infimum by applying the supremum result to $-f$]
The function $-f: X \to \mathbb{R}$ is continuous (as the composition of $f$ with the continuous map $t \mapsto -t$) and $X$ is compact. By the supremum result, $-f$ attains its supremum: there exists $x_{\min} \in X$ with
\begin{align*}
-f(x_{\min}) = \sup_{x \in X}(-f(x)) = -\inf_{x \in X} f(x).
\end{align*}
Therefore $f(x_{\min}) = \inf_{x \in X} f(x)$, and $f$ attains its infimum.
[/step]