Search code examples
cprintfwarningsunsigned

C warning: Pointer targets in passing argument 1 of ‘sprintf’ differ in signedness


I have the following piece of code that writes the value of time into the temp buffer.

#include<stdio.h>

int main() {
    unsigned char temp[8];
    unsigned int time = 0x00101010;
    
    sprintf(temp, "%x", time);
}

Both temp and time are unsigned values; however, I still get the following warning:

Pointer targets in passing argument 1 of ‘sprintf’ differ in signedness

Can someone please explain why I am getting this warning and how to remove it?


Solution

  • Either do:

    #include<stdio.h>
    
    int main() {
        char temp[8];
        unsigned int time = 0x00101010;
        
        sprintf(temp, "%x", time);
    }
    

    or

    #include<stdio.h>
    
    int main() {
        unsigned char temp[8];
        unsigned int time = 0x00101010;
        
        sprintf((char*) &temp, "%x", time);
    }
    

    sprintf is defined as sprintf(char*, const char*, ...), so the first argument needs to be a signed char pointer.