C Reference String Operation: strncpy()

The function strncpy() copies up to n characters of one string to another string.

Usage:

char * strncpy( char * target, const char *source, size_t count);

If the source has less characters then is asked for in count, then the remainder is filled with ‘\0′ characters.

Note: No null-character is implicitly appended at the end of target.

This means you have to add it yourself or the source has less characters then is asked for in count.

strncpy() source code example:


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

	main()
	{
		char line[100];
		strncpy(line, "hello",3);
		line[3]='\0';
		printf ("%s\n", line);
	}

Output of the example:


	hel

This entry was posted in C Reference string.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: