why the output of sprintf is like this?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi

i have this

 

unsigned char *ATC = "mychar=" ;

int myvalue =1  ;

char temp[11];

sprintf(temp , "%s%d " , ATC , myvalue);

 

and the temp will be "mychar=11"

i want it to be "mychar=1"

 

This topic has a solution.
Last Edited: Tue. Apr 26, 2016 - 07:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is a sprintf reference: http://www.tutorialspoint.com/c_... but that's not where your problem is. You never set myvalue to anything.

 

From another thread you started: 

ki0bk wrote:
There is a sticky at the start of the forum that explains how to ask good questions so you can get good answers.
 

 

 

Last Edited: Tue. Apr 26, 2016 - 05:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I dont know how should i ask that everyone like
Is these q is so hard that want more explain?
All input and output is right here what else do u want?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your question looks fine to me.
Your code looks fine to me.
.
I am intrigued. Please post your complete compilable code. i.e. a minimal source that shows this behaviour.
Or just ZIP up your AS7 project and attach it.
.
One handy trick in this situation is to add a space or # character after your %d.
I also recommend that you use a better name than temp. How about buffer?
And that you make it big enough to accept any int e.g. -12345

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

navidrct wrote:
I dont know how should i ask that everyone like Is these q is so hard that want more explain? All input and output is right here what else do u want?

Well, I don't know what to say. Your original code as posted:

 

navidrct wrote:
unsigned char *ATC = "mychar=" ;

int myvalue = ;  <---- This is your problem

char temp[11];

sprintf(temp , "%s%d " , ATC , myvalue);  

 

and the temp will be "mychar=11"

i want it to be "mychar=1"

 

You never set myvalue to anything, it should be 0 but you said it returns 11. Then you went back and edited the code to myvalue = 1, but you still claim myvalue contains 11. What's to understand? Sorry, you are not posting working code. You are making code up and posting it untested. That's the problem. Listen to david.prentice. I'm out of here.

Last Edited: Tue. Apr 26, 2016 - 07:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Your question looks fine to me. Your code looks fine to me. . I am intrigued. Please post your complete compilable code. i.e. a minimal source that shows this behaviour. Or just ZIP up your AS7 project and attach it. . One handy trick in this situation is to add a space or # character after your %d. I also recommend that you use a better name than temp. How about buffer? And that you make it big enough to accept any int e.g. -12345

thanks very much

ill test it

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Someguy22 wrote:

navidrct wrote:
I dont know how should i ask that everyone like Is these q is so hard that want more explain? All input and output is right here what else do u want?

Well, I don't know what to say. Your original code as posted:

 

navidrct wrote:
unsigned char *ATC = "mychar=" ;

int myvalue = ;  <---- This is your problem

char temp[11];

sprintf(temp , "%s%d " , ATC , myvalue);  

 

and the temp will be "mychar=11"

i want it to be "mychar=1"

 

You never set myvalue to anything, it should be 0 but you said it returns 11. Then you went back and edited the code to myvalue = 1, but you still claim myvalue contains 11. What's to understand? Sorry, you are not posting working code. You are making code up and posting it untested. That's the problem. Listen to david.prentice. I'm out of here.

i did test my code

thank you

solved

thanks everyone

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Late to this thread but:

unsigned char *ATC = "mychar=" ;
int myvalue =1  ;
char temp[11];
sprintf(temp , "%s%d " , ATC , myvalue);

What on earth is the point of "ATC" in this anyway? Why not simply:

int myvalue =1  ;
char temp[11];
sprintf(temp , "mychar=%d " , myvalue);

 

By the way [11] is not enough; myvalue could, potentially be -32768 in which case the string is "mychar=-32768". That is 13 characters already and with the "hidden" terminating 0x00 at the end it is really 14 bytes. So I suggest temp[] needs to be [14].