For arbitrary precision in C, the shortest practical way is to use MPFR (with GMP under the hood).
This gives n digits after the decimal point, with precision limited only by memory/time (i.e., arbitrary precision).
Great constraint 👍 — readable, pure C, no external libs, arbitrary number of digits (limited by time/RAM).
This uses a classic spigot algorithm for π:
Compile/run:
If you want, I can also give an even shorter version (still readable) with fewer checks/comments.
Done — I wrote it to:
pi.cYou can compile and run with:
Here’s a tiny C program (single line in main) that prints π with n digits after the decimal point:
Compile and run:
This is very short, but note: precision is limited by long double (so it won’t give thousands of correct digits).