Printf prints too much

Nov 2, 2016 at 8:07pm
Hey together,

I want to print Frame_ID which is a char array of length 16 in a struct t:

char Frame_ID[2*8];

now I want to print it with:

printf("%s\n",t->Frame_ID);

but the output is

01101011100010110100010101100111001100100111101101101011100010110100010101100111

thats a lot more than only 16 digits

I filled Frame_ID like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	copy(b,2*8,t->Frame_ID);


...

void copy(char* first, int size, char* result){


	  for(int i = 0;i<size;i++) {
	    *result = *first;
	    ++result; ++first;
	  }


}


whats going wrong, why doesnt stop it after 16 digits?

thank u so much
Nov 2, 2016 at 8:13pm
Did you terminate the string with a null character?
Nov 2, 2016 at 9:41pm
isnt it automatically?
b is a char array, and the first 16 bits schould be copied to Frame_ID. but at the Initialisazion I set the aray to 16 bits, so isnt it automatically finished by a null character?

do I have to create an array with 17 chars, and the last with a nul caracter?

Nov 2, 2016 at 9:51pm
isnt it automatically finished by a null character

No.


do I have to create an array with 17 chars, and the last with a nul caracter?

Yes.
Topic archived. No new replies allowed.