开发者

Printf ignoring null values?

开发者 https://www.devze.com 2023-03-26 09:38 出处:网络
I am writing C code for an embedded target, Microchip PIC24, and the runtime libc for some reason 开发者_Python百科won\'t properly send \\0 values over the serial port.

I am writing C code for an embedded target, Microchip PIC24, and the runtime libc for some reason 开发者_Python百科won't properly send \0 values over the serial port.

printf("\xEE\xEE\0test");

for example sends EE EE "test", the null byte is ignored.

Has anyone a clue why this might be?

EDIT: Ermmm, never mind XD. I was using puts to send over the string.. doh'


A null character is the string terminator in C. printf stops there because the \0 is the end of the string as far as it is concerned. Use putchar(), or printf() with a format string may work:

printf("%c", '\0');

If you are getting 0xEE 0xEE test on the line as you suggest, are you sure the test isn't coming from a different printf call?


The null character signals the end of a string as far as printf is concerned. I'm surprised it sends "test" at all. You're probably better off using the write system call especially over a serial port.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号