format specifier for short integer

Posted by cateof on Stack Overflow See other posts from Stack Overflow or by cateof
Published on 2010-06-01T19:26:12Z Indexed on 2010/06/01 19:33 UTC
Read the original article Hit count: 277

Filed under:
|
|
|

I don't use correctly the format specifiers in C. A few lines of code:

int main()
{
        char dest[]="stack";
        unsigned short val = 500;
        char c = 'a';

        char* final = (char*) malloc(strlen(dest) + 6);

        snprintf(final, strlen(dest)+6, "%c%c%hd%c%c%s", c, c, val, c, c, dest); 

        printf("%s\n", final);
        return 0;
}

I want my executable to print aa500aastack and not aa500aasta

Why I am loosing 2 byte? What is the correct format specifier for an unsighed short integer?

thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c