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 support@ccsinfo.com

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
BrunoC12



Joined: 25 May 2017
Posts: 4

View user's profile Send private message

Problem using printf to print string variables
PostPosted: Thu May 25, 2017 7:56 pm     Reply with quote

I am using strtok to separate any string. Then, I save the different values in a string variable. Up to this point the program is fine.
After that, I print all the values but I can not see that. The program prints three of the four values.
I'll pass the code, any opinions will be very helpful. Thanks

Code:


#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)AK=ptr;
   ptr = strtok(0, term);
 
}
    i=0;
   printf("%s \n\r %s \n\r  %s \n\r %s \n\r ",ID, ST, OR, AK);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

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

It works for me in MPLAB vs. 8.92 simulator with vs. 5.071. Of course, I
put a while(TRUE) at the end of the program, to prevent it from going
to sleep while it's still transmitting characters. Here is the output:
Quote:

one
two
three
four
one
two
three
four

If it still doesn't work after you add a while(TRUE) statement at the end
of main(), then post a compilable test program, similar to what is shown
below:
Code:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#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)AK=ptr;
   ptr = strtok(0, term);
  }
 
i=0;
printf("%s\r%s\r%s\r%s\r", ID, ST, OR, AK);

while(TRUE);  // Prevent PIC from going to sleep
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19254

View user's profile Send private message

PostPosted: Fri May 26, 2017 7:03 am     Reply with quote

He launched two separate threads at the same time about this....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 26, 2017 7:33 am     Reply with quote

Yes, I saw that. We posted only 3 minutes apart, and your post wasn't
visible to me at the time I posted.
Ttelmah



Joined: 11 Mar 2010
Posts: 19254

View user's profile Send private message

PostPosted: Fri May 26, 2017 10:32 am     Reply with quote

Happens... Smile

Usually, one starts to reply and by the time one has typed the reply another answer has appeared!...

Perhaps one of the moderators could 'units' the threads?.
BrunoC12



Joined: 25 May 2017
Posts: 4

View user's profile Send private message

PostPosted: Fri May 26, 2017 10:43 am     Reply with quote

I tried with while(true), but the program send the same answer.
Quote:

one
two
three
four
one
two
three


I think the problem is that it doesn't save the last data in the variable AK because it prints an empty space at the end, but I do not know why
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 26, 2017 11:03 am     Reply with quote

If you read my post, I said:
Quote:

If it still doesn't work after you add a while(TRUE) statement at the end
of main(), then post a compilable test program, similar to what is shown below:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

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

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

etc.


Also post your compiler version.
BrunoC12



Joined: 25 May 2017
Posts: 4

View user's profile Send private message

PostPosted: Fri May 26, 2017 1:38 pm     Reply with quote

Oh, sorry, I had not considered it. With that code the program works correctly, but I need to do it with the 16F628a.
I'm going to try MPLAB to see if it works.
Thanks for your help.

By the way, I use PCWHD Compiler Version 5.015.
Ttelmah



Joined: 11 Mar 2010
Posts: 19254

View user's profile Send private message

PostPosted: Sat May 27, 2017 12:58 am     Reply with quote

So, post us the code that doesn't work in the 628A.
Your original code doesn't show the processor stuff. PCM_programmer has posted code that works, so now show us similar code that doesn't.

I've taken PCM_Programmer's code, changed the header to:
Code:

#include <16F628A.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOMCLR //no MCLR needed for my board
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)


and it runs fine on a 628A. Prints:
Quote:

one
two
three
four


on hyperterminal.

and just amended to say that the test was with 5.015.
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