Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sprintf() missing characters

Status
Not open for further replies.

gravi

Full Member level 2
Joined
May 30, 2006
Messages
126
Helped
10
Reputation
20
Reaction score
10
Trophy points
1,298
Location
Hyderabad, India
Activity points
2,424
memset sprintf

Hi
This is Ravi and working on Pic 18LF8722,MPLAB18 v3.00 compiler

static unsigned char PacketBuff[50];
memset(PacketBuff, 0, strlen(PacketBuff));
sprintf(PacketBuff,"<RESET BOR='%d' POR='%d' WDT='%d'/>",BORCount,PORCount,WDTCount);
Putsusart(PacketBuff);

result:
<RESET BOR='34', POR= '46' WDT='56'/>
<RESET BOR='34', POR= '46' WDT='56'/>
<RESET BOR=' ', POR= '46' WDT='56'/>
<RESET BOR='34', POR= ' ' WDT='56'/>
<REET BOR='34', POR= '46' WDT='56'/>

I observed sometimes that when I am trying to put the string on the hyperterminal, I have blank spaces in the buffer.
I am actually trying to print a packet data which is in User understandable format...I am seeing the spaces in between.

can anybody help me out on this....


Can anyone tell me why this is caused.

Thanks in adavance
Ravi
 

aProgrammer

Full Member level 2
Joined
Mar 29, 2001
Messages
149
Helped
23
Reputation
44
Reaction score
13
Trophy points
1,298
Location
Syldavia
Activity points
611
you should not use strlen(PacketBuff) because at this time the content
of PackBuff is undefined


use

sizeof(PacketBuff)
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
What aProgrammer said. strlen() will return a garbage count for uninitialized memory (needs to find an ASCII string terminated with a zero).
 

newelltech

Full Member level 4
Joined
Aug 23, 2006
Messages
216
Helped
34
Reputation
68
Reaction score
2
Trophy points
1,298
Activity points
2,929
What aProgrammer and jhbbunch said and -
Why use memset? Sprintf() will terminate the string with a null so you don't need to clear the array. And Putsusart is only sending up to the null.
I'd just remove the memset line.
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
Yeah, what newelltech said. You can't be too efficient when it comes to C programming. Redundant or superfluous calls can be disiastrous when coding for small memory models like 8 bit micros. I can remember using sprintf() and printf() calls routinely in the same DOS program with TurboC 3.0. I found out real quick just how much memory I was wasting when I tried the code in an 8051. Lesson learned: use sprintf() only.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top