30+ years programming · Author has 657 answers and 3.3M answer views · 6y ·
It is common knowledge that C is Turing complete. For years I have believed this.
I have had bad experiences with “common knowledge” so let’s employ critical thinking, and write a Turing machine simulator in C.
- We need a state machine.
Check. It is trivial to implement a state machine in C. - We need an infinite tape, and a way to maintain a current position into it.
Let’s examine some possibilities: - RAM: Although RAM is finite, C does not seem to limit RAM size. RAM size is obviously just an implementation detail, not part of C.
But is it?
Pointers have to be convertible to size_t integers, i.e., pointers have a predefined size. The size is set at compile time. This means that we limit the tape to 2 ** num_bits(size_t) entries, which is not infinite. Even if we define size_t to have 1024 bits, it is still finite. - Disk: It seems to be OK. C library function fseek() is needed to travel the tape on disk. Obviously, neither SEEK_SET nor SEEK_END will do, since they use
long int
for offset. Sincelong int
has a predefined number of bits, the tape is finite from the start.
I’m desperate, so I look into SEEK_CUR. With SEEK_CUR it is possible to do relative seeks, without limiting the position to fitlong
. This means that there is no limit to the tape length, from language perspective.
So, C does seem to be Turing complete, but only due to a loophole in fseek(). Without using the loophole of relative file seek, it would have not been Turing complete.
2K views ·
View upvotes
· 1 of 6 answers
Something went wrong. Wait a moment and try again.