strncat in c
strncat in c programming

If you want to know about strncat in c then read this blog post.

What is strncat in c programming?

strncat in c appends source string to the end of a destination string upto n characters long.

strncat returns pointer to the destination string.

It is defined in string.h header file.

Prototype

char *strncat(char *destination,const char* source,size_t n);

Parameters for strncat in c

strncat takes three arguments. The destination string, source string and n number of characters.

Return value of strncat in c

It returns pointer to the destination string.

strncat example program in c

Example 1

//strncat exanple in c programming
#include<stdio.h>
#include<string.h>
int main()
{
  char src[40]="Hello"; dest[40]="This is scholarsoul";
  strncat(dest,src,5);
  printf("Resulting destination string is : %s \n",dest);
  return 0;
}
strncat in c example 1
strncat in c example 1

Code explanation

The source string is appended to the destination string up to n characters long after running strncat function. The result is printed onto the console.


Example 2

//strncat exanple in c programming
#include<stdio.h>
#include<string.h>
int main()
{
  char src[40]="World", dest[40]="Hello";
  printf("Before using function destination string is :%s \n",dest);
  strncat(dest,src,5);
  printf("Resulting destination string is :%s \n",dest);
  return 0;
}
strncat in c example 2
strncat in c example 2

Code explanation

The source string is appended to the destination string up to n characters long after running strncat function. The result is printed onto the console.


Difference between strncat and strcat in c

strncat appends source string to the destination string up to n characters. strcat in c appends the source string to the destination string.

strncat also returns pointer to the destination string. strcat returns pointer to the string destination.

strncat takes three arguments. strcat takes two arguments.

strncat appends only specified characters to the destination. strcat appends entire source string to the destination.

General forms are as follows

char *strncat(char *dest,const char*source,size_t m);
char *strcat(char *dest,const char*source);
strncat in cstrcat in c
Appends source string to the destination string upto n characters.Appends source string to the destination string.

char *strncat(char *dest,const char*source,size_t m);
char *strcat(char *dest,const char*source);
Takes three arguments.Takes two arguments.
Appends only specified characters to the destination.Appends entire source string to the destination.

Difference between strlen and strncat in c

strlen in c is used to calculate the length of a string. strncat in c appends source string to the end of a destination string up to n characters long.

strlen returns total length of a string. strncat returns pointer to the destination string.

strlen terminates when it encounters null character. strncat terminates when process is over.

strlen takes one argument. strncat takes three arguments.

General forms are as follows

int  strlen(const char*string);

char *strncat(char *destination,const char* source,size_t n);
strlen in cstrncat in c
Used to calculate the length of a string.Appends source string to the end of a destination string upto n characters long.
int strlen(const char*string);char *strncat(char *destination,const char* source,size_t n);
Returns total length of a string.Returns pointer to the destination string.

Difference between strcat and strncat in c

strcat in c appends the source string to the destination string. strncat in c appends source string to the end of a destination string up to n characters long.

strcat returns pointer to the string. strncat returns pointer to the destination string.

strcat takes two arguments. strncat takes three arguments.

Prototypes are as follows

char *strcat(char *destination ,const char*source);
char *strncat(char *destination,const char* source,size_t n);
strncat in cstrcat in c
Appends source string to the end of a destination string upto n characters long.Appends source string to the destination string.
char *strncat(char *destination,const char* source,size_t n);
char *strcat(char *destination ,const char*source);
Returns pointer to the destination string.Returns pointer to the string.

Difference between strcmp and strncat in c

strcmp in c is used to compare two strings. strncat in c appends source string to the end of a destination string up to n characters long.

strcmp returns zero,negative,positive values depending on result. strncat returns pointer to the destination string.

strcmp takes two arguments. strncat takes three arguments.

strcmp returns integer value. strncat returns pointer to the string.

General forms are as follows

int strcmp(const char* string1,const char* string2);

char *strncat(char *destination,const char* source,size_t n);
strcmp in cstrncat in c
Used to compare two strings.Appends source string to the end of a destination string upto n characters long.
int strcmp(const char* string1,const char* string2);char *strncat(char *destination,const char* source,size_t n);
Returns integer value.Returns pointer to the destination string.

Difference between strncat and strcpy in c

strcpy is used to copy the source string to the destination string. strncat in c appends source string to the end of a destination string up to n characters long.

strcpy returns pointer to the destination string. strncat returns pointer to the destination string.

strcpy takes two arguments. strncat takes three arguments.

General forms are as follows

char *strcpy(char *destination ,const char*source);

char *strncat(char *destination,const char* source,size_t n);
strcpy in cstrncat in c
Used to copy the source string to destination string.Appends source string to the end of a destination string upto n characters long.
char *strcpy(char *destination ,const char*source);char *strncat(char *destination,const char* source,size_t n);
Returns pointer to the destination string.Returns pointer to the destination string.

People also ask for

What is the difference between strncat and strrev in c?

strncat in c appends source string to the end of a destination string up to n characters long. strrev is used to reverse the given string.

What is the difference between strncat and strchr in c?

strncat in c appends source string to the end of a destination string up to n characters long. strchr is used to search for the first occurrence of a character in the pointed string.

Is strncat safe?

strncat is safe compared to strcat in c. strcat appends the entire string and can cause a buffer overflow.


People also read