为什么我在宏中获取特定基本数据类型的printf格式?

new*_*erl 2 c macros

如何写FORMAT_OF(type),所以,这将是%dint,%schar *,等等.

是否有宏来获取基本数据类型的格式?

Die*_*Epp 5

是.但它仅适用于C11.

#define FORMAT_OF(x) _Generic((x), \
    int: "%d", \
    unsigned: "%u", \
    const char *: "%s", \
    void *: "%p")
Run Code Online (Sandbox Code Playgroud)

对于类型而不是值,您可以尝试_Generic((type) 0, ....

您的编译器可能不支持C11.GCC 4.6(但不是4.5!)使用-std=c1x或启用了一些C11支持-std=gnu1x,但我认为_Generic还不支持.