will a C extension built with c99 or previous versions of C compiler work with python3 built with c11? Just creating a .so and then doing an import of .so module will work?
The concept you are asking about is called ABI (application binary interface) compatibility, and it is not a trivial question to answer.
First of, there seems to be some confusion of terminology. c99 and c11 are not C compilers, they are C language standards. Whether a piece of software is written in c99, c11, or any other C variant does not affect its ABI. A C extension written in (not built with) c99 can absolutely be binary compatible with python3.11+, which is written in c11. But is not guaranteed to be so, because language standards are orthogonal to ABI compatibility.
Whether two pieces of software are ABI compatible is difficult to answer in general. The only way to be certain is to compile the two softwares with the same compiler, and link them against the same version of any libraries used in both.
However, it is possible that the softwares could be ABI compatible even if they are built with different compilers and linked against different versions of the same libraries. It is just about impossible to prove this to be the case, however. Even if everything seems to run fine, you never know when you will run into an edge case where the ABI breaks down.
Tl;dr: Maybe. The only way to be certain is to compile the extension with the exact same toolchain that built python.
Thank you @abessman.
I tried to build a small C extension which just does a print, I first built it with c11 using Python.h of python3.12 and tried the same with c89 using the same Python.h, the compilation failed due to syntax errors. I assume the compilation will fail in most cases due to Python.h compatibilty. This is not an issue however, since python3.11+ follows c11 standards. It would be brainstorming to think of some of the working cases though.
This is because C standards are neither backward nor forward compatible. Valid C89 code is not (guaranteed to be) valid C11 code, and vice versa. CPython3.11+ is written in C11, and therefore cannot be compiled with --std=c90.
This does impose a limitation which I didn’t think of above. As you found, since Python.h is of course also written in C11 it cannot be included in an extension written in C89. So effectively, extensions for CPython3.11+ must be written in C11, or a subset of C89 which is also a subset of C11.