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

Problem using printf to print string variables

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



Joined: 12 Feb 2004
Posts: 9
Location: Surrey, UK

View user's profile Send private message Visit poster's website

Problem using printf to print string variables
PostPosted: Thu Mar 25, 2004 6:26 am     Reply with quote

I am using

printf("%S",StringData);

where StringData is a string variable (character array with null terminator)

What I get on the RS2323 output is only the last two characters!

For instance, if I load StringData as follows:

StringData[0]=0x31; //ASCII 1
StringData[1]=0x32; //ASCII 2
StringData[2]=0x33; //ASCII 3
StringData[3]=0x34; //ASCII 4
StringData[4]=0; //null

Output is 34

If I use the form

printf("1234");

Then I get 1234 as expected.

Anyone any idea what's going on? I have checked the file registers and the string variable is correctly loaded, so why does it ignore all but the last 2 characters? Confused
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 25, 2004 2:09 pm     Reply with quote

The following program works fine with PCM vs. 3.184.
It displays this: 1234

Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main(void)
{
char StringData[5];

StringData[0]=0x31; //ASCII 1
StringData[1]=0x32; //ASCII 2
StringData[2]=0x33; //ASCII 3
StringData[3]=0x34; //ASCII 4
StringData[4]=0; //null

printf("%S",StringData);

while(1);
}
Peter F



Joined: 12 Feb 2004
Posts: 9
Location: Surrey, UK

View user's profile Send private message Visit poster's website

PostPosted: Fri Mar 26, 2004 2:13 am     Reply with quote

Thanks for that! My code is exactly the same. Only differences are that I am using PCH with the 18F452 chip. Using puts(StringData); gives the same result. I have emailed CCS to see if they can help. If I can't find a way out, I will have to write my own function to output strings.
BrunoC12



Joined: 25 May 2017
Posts: 4

View user's profile Send private message

Same problem
PostPosted: Thu May 25, 2017 8:51 pm     Reply with quote

I have the same problem. The program doesn't print all of the values that i define on printf.
Code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char ID[], ST[], OR[], AK[];

void main()

   char string[30], term[4], *ptr;
   int i;
 

strcpy(string,"one,two,three,four");

strcpy(term,",;");

i=0;

ptr = strtok(string, term);

while(ptr!=0) {

  i++;
  puts(ptr);
   if(i==1)ID=ptr;
   if(i==2)ST=ptr;
   if(i==3)OR=ptr;
   if(i==4)strcpy(AK,ptr);
   ptr = strtok(0, term);
 
}
     
   printf("%s \n\r %s \n\r  %s \n\r %s \n\r ",ID, ST, OR, AK);

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Fri May 26, 2017 1:09 am     Reply with quote

Are you just losing the last couple of characters?.

If so, not the same problem.

What is happening, is your code drops 'off the end', and the characters still waiting to be sent, then get lost.

Look at PCM_programmer's example in this thread. The 'while' at the end of the code is to prevent this happening.

You have to realise that with embedded code, there is no 'OS' sitting outside the code, waiting to handle things when you leave. The code you write is _everything_. If your code runs out the end, then there is nothing there 'left' to do jobs at this point....
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