Sadly Knuth in his wisdom gave only few clues in the TeXBook for cases like this.
In order to understand the error we need to firstly understand the definition of
\the, which is for the case of
\the<codename><8-bit number>, where
<codename> stands for either
So clearly in this case
catcode TeX expects a number and hence the error generated in the example provided by the OP.
All solutions provided by the other posts revolve around changing the definition one way or another to produce the required character code number and which I am demonstrating here with some different examples:
The example below will produce the right answer in both cases,
% results category 11
% results category 12
While comparing two tokens using
\ifcat things become a bit more complicated, if you want to compare two active characters you have to say
\noexpand - Knuth says so somewhat obscurely in Exercise 20.11!
Consider the following definitions
The following will result True since we comparing
[ with ']' both now being category 13
\ifcat\noexpand[\noexpand] True \else False \fi
\ifcat[* True \else False \fi is True
Since now we have established two facts
\the\catcode needs a number and if you use an active character, implicitly or explicitly we can understand why the following will all work!
or Joseph's suggestion for comparisons: