Wrong output sprintf C51 8051 MCU embedded system

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Wrong output sprintf C51 8051 MCU embedded system



I have the following fragment of C code for an AT89S52 microcontroller:


unsigned char x = 0x10;
unsigned char str[21];

sprintf(srt, "%u", (x >> 4) );



Can someone tell me why does my string contain "256" instead of "1" ?, the x variable isn't even big enough to hold the 256 value.



I'm compiling with KEIL uVision 5, using the standard stdio.h library.



If I use:


void UlToStr(char *s, unsigned long bin, unsigned char n)

s += n;
*s = '';

while (n--)

*--s = (bin % 10) + '0';
bin /= 10;




instead of sprintf the string holds a 1 as a value.





I find it hard to believe that this code is behaving as described.
– Eugene Sh.
1 hour ago





Are you sure that shift is what you show us? Or the initial value of x?
– Some programmer dude
1 hour ago


x





How do you know what the string is containing?
– Eugene Sh.
1 hour ago





Also, when passing values of smaller types (like char or short) to a vararg function (like printf) those values will be promoted to int or possible unsigned int.
– Some programmer dude
1 hour ago


char


short


printf


int


unsigned int





@Someprogrammerdude The result of >> is already int.
– Eugene Sh.
1 hour ago


>>


int









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

make 2 or more post in bootsrap

Dynamically update html content plain JS