Home > OS >  About byte string sprintf and printf. Excuse me, is what reason, how to achieve desired results
About byte string sprintf and printf. Excuse me, is what reason, how to achieve desired results

Time:09-23

Char data [10]={0 x31, 0 x31, 0 x33, 0 x2e, 0 x31, 0 x32, 0 x33, 0 x34, 0 x35, 0 x36};
Char buf [128]={0};
Printf (" % s \ n ", data);
Sprintf (buf, "% s", data);
Printf (" % s ", buf);


The expected result is:
113.123456
113.123456

The results of the actual output is:
113.1234563131332 e313233343536
113.1234563131332 e313233343536

Question:
1 what the reasons for this problem is
If the modified code above the desired results

CodePudding user response:

The compiler you have any question, you what distribution system? What the compiler? Is not at the end add a 0 x0
 
Char data [11]={0 x31, 0 x31, 0 x33, 0 x2e, 0 x31, 0 x32, 0 x33, 0 x34, 0 x35, 0 x36, 0 x0};

CodePudding user response:

A byte string class better for array than the last opening deposit '\ 0', or print behind it
  • Related