View previous topic :: View next topic |
Author |
Message |
baris_akinci
Joined: 10 Mar 2016 Posts: 17 Location: Ankara
|
problem on sending string by using fputs |
Posted: Tue Sep 20, 2016 1:54 pm |
|
|
Hello, I need help to understand what is the main reason CCS C put extra value when I try to send string by using fputs via rs232 connection. the code belock is below
Code: |
char gonder[3];
char gecici;
char gonder[4];
if (time <= 13) { gecici='0'; }
else { gecici='1'; }
gonder[0]=gecici;
gonder[1]=sifre[2]; // entered pasword part -> 1
gonder[2]=sifre[3]; // entered pasword part -> 4
fputs(gonder);
|
I just want to see 114 instead of 114r. where this "r" comes from?? see the picture below. thanks in advance.
[IMG]
https://resmim.net/f/dhJlMv.png
[/IMG] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Tue Sep 20, 2016 2:13 pm |
|
|
You are lucky to only get an extra 'r'...
This is not anything special about CCS, but standard 'C'.
In 'C', a 'string' is a "null terminated array of characters". You are not adding the null terminator. fputs, will keep printing, until it sees such a terminator, so what you get 'extra' will depend on what happens to be in memory.....
You need to add:
gonder[3]='\0';
to terminate the array, before you try to print. |
|
|
baris_akinci
Joined: 10 Mar 2016 Posts: 17 Location: Ankara
|
problem on sending string by using fputs |
Posted: Wed Sep 21, 2016 12:45 pm |
|
|
thank you so much Ttelmah. I have tried your advice and got the result well. |
|
|
|