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.
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
Video: Close-up of the destructive power of a 12th-century catapult
The world's oldest and most famous gaming magazine has just closed
The world's widest boulevard with 16 lanes was completed half a century ago
Korea stirs because robot employee 'self-destructs'
Billionaire Musk's SpaceX took on the job of destroying the ISS
Nostradamus and prophecies about the world's destiny in 2021