1
votes

I have this code:

PRINT_VALUE("Displaying value: %x",A->B.C.D);

D is of type unsigned short.

#define  PRINT_VALUE(Name,Val)    \
    if(Val){fprintf(fp,Name,Val);fprintf(fp,"\n");}\
    else {fprintf(fp,Name);fprintf(fp,"\n");}

Even if I use %d and cast A->B.C.D to int, the same warning persists. Any help?

Warning message:

warning: format ‘%x’ expects a matching ‘unsigned int’ argument [-Wformat]

1

1 Answers

2
votes

You have a problem with macro expansion. I had a similar code.

#define fp stdout

#define  PRINT_VALUE(Name,Val)    \
    if(Val){fprintf(fp,Name,Val);fprintf(fp,"\n");}\
    else {fprintf(fp,Name);fprintf(fp,"\n");}

int main()
{
        short D = 0;
        PRINT_VALUE("Displaying value: %x", (unsigned int)D);
}

This macro expands as follows

int main()
{
    short D = 0;
    if((unsigned int)D)
    {
        fprintf(stdout,"Displaying value: %x",(unsigned int)D);
        fprintf(stdout,"\n");
    }
    else
    {
        fprintf(stdout,"Displaying value: %x"); //<-- the warning is here
        fprintf(stdout,"\n");
    };
}

The else part also requires an argument. Probably that is not what you want your macro to do.