The strncpy () function in C
Strncpy char * (char * dest, const char * src, size_t n) copies up to n copies to n characters from the string pointed to by src to dest. In case the length of the src is smaller than n, the remainder or the remainder of dest will be filled with null values.
Strncpy char * (char * dest, const char * src, size_t n) copies up to n copies to n characters from the string pointed to by src to dest. In case the length of the src is smaller than n, the remainder or the remainder of dest will be filled with null values.
Declaring strncpy () function in C
Here is the declaration for strncpy () in C:
char * strncpy ( char * dest , const char * src , size_t n )
Parameters
dest - This is the pointer to the destination array, where the content is copied.
src - The string is copied.
n - Number signed me to be copied from src.
Returns the value
This function returns the last copy of the copied string.
For example
The following program C illustrates the usage of strncpy () function. Here we have used the memset () function to clear the memory location.
#include #include int main () { char src [ 40 ]; char dest [ 12 ]; memset ( dest , '' , sizeof ( dest )); strcpy ( src , "Hoc C co ban va nang cao tai QTM !!!" ); strncpy ( dest , src , 10 ); printf ( "Sau khi thuc hien ham strncpy, chuoi dest co dang: n%sn" , dest ); return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous lesson: strcpy () function in C
Next lesson: strcspn () function in C
You should read it
- The strlen () function in C
- The strcspn () function in C
- The strchr () function in C
- The strerror () function in C
- Strncat () function in C
- Memset () function in C
- How to 'survive' with the stress of learning programming
- Bookmark a few tricks to help you master a programming language
- The strrchr () function in C
- Top 20 free programming learning websites need to bookmark immediately!
- Beginners of computer programming need to focus on what?
- Strcpy () function in C
Maybe you are interested
Nvidia just caused the 'blue screen of death' error for Windows Admire incredibly detailed images of the Orion Nebula through the eyes of the James Webb telescope This star 'tears up' its companion to create a rare beautiful nebula Admire the moment millions of new stars are born in the Lupus 3 nebula Before 'returning to Thanos', what was the fate and appearance of Nebula in the comics? How to create a blue screen of death (BSOD) to 'prank' friends