CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sprintf "bug" or my misunderstand of c?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

sprintf "bug" or my misunderstand of c?
PostPosted: Thu Dec 22, 2005 4:11 am     Reply with quote

Basically dummy can be populated with a 4 character string terminated with \0. This works for 1,2,3, and 4 characters perfectly. However if I don't have any characters...for example:

char dummy[5];
char line1[17]

dummy[0] = '\0';
sprintf(line1,"%S",dummy);

this doesn't seem to do what I want it to do and null terminate the string, i.e. make line1[0] = '\0' but it doesn't do this. The line1 array still contains the original contents and isn't touched by the sprintf in this case.

However:

dummy[0] = 'A';
dummy[1] = '\0';
sprintf(line1,"%S",dummy);

correctly populates line1 with 'A' '\0'

I am wondering if this a C feature or a compiler "feature". I am using 3.241
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Dec 22, 2005 4:33 am     Reply with quote

I am going to revise my question a little:

sprintf(line1,"");

or indeed:

sprintf(line1,"\0");

doesn't do what I hope which is

line1[0] = '\0';

Accoring to my understand of c The sprintf() function places output followed by the null byte, '\0'

If there isn't anything to place in the string should it still place \0 ? The CCS compiler doesn't (at least not my version) should it?
Ttelmah
Guest







PostPosted: Thu Dec 22, 2005 5:56 am     Reply with quote

Really it is just a case of trying to do something the function is not designed/guaranteed to handle. If you pull the Linux sprintf manual page (on the version I have at least), it makes the comment, that 'the results are undefined if an argument has zero size". This suggests that other compilers also exhibit this sort of behaviour.
So I'm afraid the Linux manual says it all. This is an 'undefined' state.
I'd actually suspect (it'd be worth looking at the listiing file generated), that the optimiser, is seeing that there is no argument, and is optimising the call out of existence.

Best Wishes
Guest








PostPosted: Thu Dec 22, 2005 8:42 am     Reply with quote

I see what you are saying. I don't think it's an optimisation problem as the second "dummy" string array is created dynamically by other parts of the code. It's only when it's been loaded with '\0' as the first character that the sprintf problem happens! Took me a while to spot it :-) I will set a flag to cover the problem of "dummy" array being zero length.

I don't think the old way of making your own sprintf command and printf'ing to it will help as I suspect printf will also ignore an '\0' as the first byte.
Ttelmah
Guest







PostPosted: Thu Dec 22, 2005 9:59 am     Reply with quote

Yes.
Of course if the compiler allowed you to generate variable length argument lists for your own functions, it'd be a 'doddle', to just encapsulate the sprintf, into your own 'checked_sprintf' function that did this. This is one of the 'weaknesses' of the compiler... :-(

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group