First question is about null char \0 at the end of string, there are so many variations in terms of when \0 is needed/auto-added.
When declaring a char array, do I need to specify \0 or no? or in what case, should I specify \0, what case not? Can someone give a comprehensive summary? (as in this post). if you feel my question is ambiguous, then a more specific one is when declare a string in C, what is the best way, is it char string[] = "first string", because for example, in this way, I can do strcat(string, another_string) without concern about size problem?
Second question: I have
1   char a[] = "kenny";
2   char b[3];
3   strncpy(b, a, (int)(sizeof(b) - 1));
4   printf("%i\n", (int)sizeof(b)); // 3
5   printf("string length: %i\n", (int)strlen(b)); // string length: 8
6   printf("%s\n", b); // give me random stuff like kekenny or keEkenny 
- 3: I only want to pass 2 bytes to b
- 4: sizeofbehaves normally
- 5: but why does it become 8???
- 6: why does it give me random stuff like kekenny or keEkenny
I just got lost what is happening in C string. I used to use C++ a lot but still can't understand how C string behaves.
 
     
     
     
     
     
    