strncpy() example

Hi All,

This example is on the strncpy() webpage but it is as follows:

/* strncpy example */

//strncpy(destination,source,number)

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

int main ()
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);

//str2[6] = "to be or not to"

str2[5]='\0';

//str2[6] = "to be or not \0"

puts (str2);
return 0;
}

the comments are added by me for my logic. the output states that it should be "to be" but when i go through it myself, it reads "to be or not". why should the "or not" be taken out of str2?

Thanks,
jrobot
Erm, what? The strncpy call copies the first five characters from str1 to str2, which is "To be".
The commented lines don't make any sense.
pardon my rustiness. i was considering each word to be an entry instead of each individual character. makes more sense now. thanks!
Topic archived. No new replies allowed.