"hh" Modifer


#include <stdio.h>

void main()
{
    const char *get_mac = "1A267D0B1124";
    unsigned int block2[6] = {0};
    char final[13] ="0";

    sscanf(get_mac, "%02X%02X%02X%02X%02X%02X \t", &block2[0],&block2[1],&block2[2],&block2[3],&block2[4],&block2[5]);
    snprintf(final, sizeof(final),"%02X%02X%02X%02X%02X%02X \t", block2[0],block2[1],block2[2],block2[3],block2[4],block2[5]);
    printf("efinal  %s  \t\n",final);
    return;
}


Output : 

efinal  1A267D0B1124   

Why we need hh modifier ??


When your unsigned int changes to unsigned char, you might have seen this warning often in your program. Let's see why we need to use "hh" modifier in sscanf  to remove this warning.

#include <stdio.h>

void main()
{
    const char *get_mac = "1A267D0B1124";
    unsigned char block2[6] = {0};
    char final[13] ="0";

    sscanf(get_mac, "%02X%02X%02X%02X%02X%02X \t", &block2[0],&block2[1],&block2[2],&block2[3],&block2[4],&block2[5]);
    snprintf(final, sizeof(final),"%02X%02X%02X%02X%02X%02X \t", block2[0],block2[1],block2[2],block2[3],block2[4],block2[5]);
    printf("efinal  %s  \t\n",final);
    return;
}


warning: format ‘%X expects
> argument of type unsigned int *’, but argument 3 has type unsigned
> char *’ [-Wformat=] 

You need to tell sscanf that arguments are of char type. This is done by putting hh modifier before the X format specifier.

Using hh modifier 


#include <stdio.h>

void main()
{
    const char *get_mac = "1A267D0B1124";
    unsigned char block2[6] = {0};
    char final[13] ="0";

    sscanf(get_mac, "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX \t", &block2[0],&block2[1],&block2[2],&block2[3],&block2[4],&block2[5]);
    snprintf(final, sizeof(final),"%02X%02X%02X%02X%02X%02X \t", block2[0],block2[1],block2[2],block2[3],block2[4],block2[5]);
    printf("efinal  %s  \t\n",final);
    return;
}


Output :

efinal  1A267D0B1124  

Comments