weird behavour with sprintf

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

using this line of code

   

strcpy(output_buffer+output_counter,"Hello World\r\n");

   

output_counter += 15;

   

output_counter += sprintf(output_buffer+output_counter, "%d\r\n", 60);

   

I should get an output of "Hello World\r\n60\r\n" but instead im getting an output of  "Hello World\r\n15536"

0 Likes
3 Replies
Anonymous
Not applicable

if I make it 

   

strcpy(output_buffer+output_counter,"Hello World\r\n");
output_counter += 15;

   

output_counter += sprintf(output_buffer+output_counter, "%d\r\n", 100);

   

I get "Hello World\r\n25776\r\n"
 

0 Likes
Anonymous
Not applicable

if I change the value to a long I get the correct output, ie

   

strcpy(output_buffer+output_counter,"Hello World\r\n");
output_counter += 15;

   

output_counter += sprintf(output_buffer+output_counter, "%ld\r\n", 100l);

   

I get "Hello World\r\n100\r\n"

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The numbers 60 and 100 can and will be represented by an uint8, while the %d format expects an integer. All will go well when you use 60u and 100u.

   

 

   

Bob

0 Likes