Print out type of literal or variable in C

References:

Was trying to find out the type of char/string literals in C. Found a hackish way of doing it.

Source code: test.c

#include <stdio.h>

int main()
{
    char *s = "ABC";
    printf("%f\n", s);

    printf("%f\n", 'A');
    printf("%f\n", "Hello World");

    printf("%f\n", 42);
    printf("%s\n", 42.0);
}

Compile with warning flag: gcc -Wall test.c

test.c: In function 'main':
test.c:6:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'char *' [-Wformat=]
     printf("%f\n", s);
            ^
test.c:8:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
     printf("%f\n", 'A');
            ^
test.c:9:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'char *' [-Wformat=]
     printf("%f\n", "Hello World");
            ^
test.c:11:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
     printf("%f\n", 42);
            ^
test.c:12:12: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'double' [-Wformat=]
     printf("%s\n", 42.0);
            ^

Basically, use an incompatible format specifier for the literal/variable and let the compiler catch it. In the example above, the type for “argument 2” is indicated in the warnings.