I tried to write hexadecimal value of int8_t variable to a buffer with the following code:
#include <stdint.h>
#include <stdio.h>
int main(){
char buf[16];
uint16_t val = 0xDEAD;
int8_t val2 = -50;
sprintf(buf, "%04x%02x", val, val2);
printf("%s\n", buf);
sprintf(buf, "%04x%02x", val, (uint8_t)val2);
printf("%s\n", buf);
return 0;
}
And the output is as follows:
deadffffffce
deadce
How is the first result generated?
I compiled with: gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005