w3resource

C strncat() function

C strncat() function - concatenate a string with part of another

C strncat() function (string.h): The strncat() function is used to append the first count characters of string2 to string1 and ends the resulting string with a null character (\0).

Use strncat() function when:

  • Concatenating part of a string rather than the entire string, such as when building strings in segments or avoiding buffer overflow.
  • Working with limited buffer sizes where you want to control how many characters are appended.
  • Concatenating a specific substring, making it useful when only a part of the source string is relevant.

Syntax:

char *strncat(char *string1, const char *string2, size_t count);

Parameters:

Name Description Required /Optional
string1 Null-terminated destination string. Required
string2 Null-terminated source string. Required
count Number of characters to append. Required

Return value from strncat()

Returns a pointer to the destination string. No return value is reserved to indicate an error.

Examples: strncat() function

Example 1: Appends only a specified number of characters

The following example demonstrates how strncat() appends only a specified number of characters. The program initializes string1 to “computer” and then appends only the first 3 characters of " program" to it, resulting in "computer pro". This highlights strncat()'s use in appending only a portion of a string to manage buffer size and content precisely.

Code:

#include <stdio.h>
#include <string.h> 
#define SIZE 40
 
int main(void)
{
  char string1[SIZE] = "C";
  char * ptr;
 
  /* Reset string1 to contain just the string "computer" again */
 
  memset( string1, '\0', sizeof( string1 ));
  ptr = strcpy(string1, "computer" );
 
  /* Call strncat with string1 and " program" */
  ptr = strncat( string1, " program", 3 );
  printf( "strncat: string1 = \"%s\"\n", string1 );
} 

Output:

strncat: string1 = "computer pr"

Example 2: Difference between strcat() and strncat()

The following example demonstrates the difference between strncat() and strcat(). The strncat() function appends only the specified number of characters in the second string to the first whereas the strcat() function appends the entire second string to the first.

Code:

#include <stdio.h>
#include <string.h> 
#define SIZE 40
int main(void)
{
  char string1[SIZE] = "C";
  char * ptr;
  /* Call strcat with buffer1 and " program" */
  ptr = strcat( string1, " program" );
  printf( "strcat : string1 = \"%s\"\n", string1 );
  /* Reset string1 to contain just the string "computer" again */
  memset( string1, '\0', sizeof( string1 ));
  ptr = strcpy(string1, "C" );
  /* Call strncat with string1 and " program" */
  ptr = strncat( string1, " program", 3 );
  printf( "\nstrncat: string1 = \"%s\"\n", string1 );
} 

Output:

strcat : string1 = "C program"

strncat: string1 = "C pr"

Example 3: Concatenating a limited part of a string to avoid overflow

This example demonstrates using strncat() to safely append part of a string to another string while preventing overflow in a limited buffer.

Code:

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

#define SIZE 20

int main() {
    // Define a buffer with limited space
    char destination[SIZE] = "Welcome";
    char source[] = " to the C programming tutorial!";

    // Append only the first 10 characters of source to destination
    strncat(destination, source, 10);

    // Display the result
    printf("Result: %s\n", destination);

    return 0;
}

Output:

Result: Welcome to the C

Explanation:

  • A destination string "Welcome" and a source string " to the C programming tutorial!" are defined.
  • strncat() appends only the first 10 characters from source to destination, resulting in "Welcome to the C".
  • The limited addition prevents overflow, ensuring the buffer remains within its defined size.

Example 4: Adding a file extension to a filename

This example demonstrates how to use strncat() to add a specific part of a string, such as a file extension, to a base file name.

Code:

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

#define SIZE 30

int main() {
    // Define a base filename
    char filename[SIZE] = "report";
    char extension[] = ".pdf2023";

    // Append only the first 4 characters of extension to filename
    strncat(filename, extension, 4);

    // Display the complete filename
    printf("Complete Filename: %s\n", filename);

    return 0;
} 

Output:

Complete Filename: report.pdf

Explanation:

  • The filename string is initialized as "report", and extension holds ".pdf2023".
  • strncat() appends only the first 4 characters of extension (".pdf") to filename, resulting in "report.pdf".
  • This approach is useful for ensuring only the necessary extension is added without any extra or unwanted characters.

 

C Programming Code Editor:

Previous C Programming: C strcat()
Next C Programming: C strchr()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/string/c-strncat.php