Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
strcat

Concatenating two strings in C

Educative Answers Team

In C, the strcat() function is used to concatenate two strings.

Prototype declaration

The strcat() function is declared as follows:

strcat(char *dest, const char *src)

How it works

The function takes two strings as input; the destination string, and the source string. The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.

Look at the following illustrations to get a better idea of how the strcat() function works:

1 of 3

Example

The following pieces of code will concatenate two strings using the strcat() function:

#include <stdio.h>
#include <string.h>
int main()
{
    char destination[] = "Hello ";
    char source[] = "World!";
    strcat(destination,source);
    printf("Concatenated String: %s\n", destination);
    return 0;
}

In addition to modifying the original destination string, the strcat() function also returns a pointer to that string.

Therefore, we can directly pass the strcat() function to the printf() function.

#include <stdio.h>
#include <string.h>
int main()
{
    char destination[] = "Hello ";
    char source[] = "World!";
    printf("Concatenated String: %s\n",  strcat(destination,source));
    return 0;
}

RELATED TAGS

c
strcat
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring