Prove that the given subset satisfying the given hypothesis is compact. Let C be a subset of a compact metric space (X, d). Assume that, for
every continuous function h : X → R, the restriction of h to C attains a
maximum on C. Prove that C is compact.
My attempt:
I intend to show that every infinite subset of C has a limit point in C.
Let C' be an infinite subset of C. Since X is compact , C' has a limit point , say q, in X. Suppose q is not in C. Then I am trying to obtain some contradiction to the hypothesis i.e construct a continuous function whose restriction to C has maximum at q. But then q must be in C. I think there is something wrong in the last two lines. But this is what I have tried.
Apart from your own methods, if someone can provide a proof along the lines of the above approach then please do post it. 
Thanks.

Suppose $C$ is a subset of a compact metric space $(X,d)$. Furthermore, assume that every continuous function from $X$ into $\mathbb{R}$ when restricted to $C$ obtains its maximum on $C$.
If $C$ is finite, then $C$ is compact. So we may assume the case $C$ is infinite.
Since $C$ is an infinite subset of the compact space $X$, it has a limit point in $X$, say $q$. Consider the function $f:X\rightarrow \mathbb{R}$ defined by $f(x)=\frac{1}{1+d(x,q)}$. This function obtains its maximum at $d(x,q)=0$ (i.e. $x=q$). Since $q$ is a limit point of $C$, there is a sequence $(x_n)_{n=0}^\infty$ of points in $C$ which converge to $q$. Since $f$, when restricted to $C$, must obtain its maximum by assumption, we have that $q\in C$.
