Simple examples of using strcat to join two strings together in C

The most common example you will find on the internet will work by just making an array that is "big enough" to store both the strings. However this can create problems when the strings you use are too long so it would need these conditions handled correctly in C. So it probably isn't the best example to use.

void basic(char *s1, char *s2)
{
        char str[200] = "";

        strcat(str, s1);
        strcat(str, s2);
        printf("basic: %s\n", str);
}

A better example of using strcat is to allocate an array dynamically. In this case on the stack. So this will work slightly better however it can cause a significant amount of stack space to be used and can also cause crashes if the strings are too long. However it will execute very quickly because dynamic allocation of memory on the stack will perform well.

void better1(char *s1, char *s2)
{
        char str[strlen(s1) + strlen(s2) * sizeof(*s1) + 1];
        str[0] = '\0';

        strcat(str, s1);
        strcat(str, s2);
        printf("basic: %s\n", str);
}

A much better example of this is to use malloc / free for the allocation of string. This will be slightly slower than the previous example but will be able to handler much larger strings and much safer to use. It can of course still crash but only if the machine run's out of memory. How ever this error can be handled to prevent it crashing by changing the call to abort() to use proper error handling.

void better2(char *s1, char *s2)
{
        char *str = malloc(strlen(s1) + strlen(s2) + 1 * sizeof(*s1));
        if (str == NULL)
                abort();
        str[0] = '\0';

        strcat(str, s1);
        strcat(str, s2);
        printf("basic: %s\n", str);

        free(str);
}

Finally this is an example about joining multiple strings so we can just keep making our string longer and longer or join it from multiple different sources. Note that the following code really needs a check around the realloc function to see if it returns NULL. If it does then it should fail by returning the previous pointer. Or call abort because the system has run out of memory.

char *multiple(char *str, char *s2)
{
        int len;
        char *s;
        if (str != NULL)
                len = strlen(str);
        len += strlen(s2) + 1 * sizeof(*s2);
        s = realloc(str, len);
        strcat(s, s2);
        return s;
}

With the above code we can call it in the following way

char *tmp = multiple(NULL, "Hello ");
        tmp = multiple(tmp, " World");
        tmp = multiple(tmp, " We");
        tmp = multiple(tmp, " Can");
        tmp = multiple(tmp, " Join");
        tmp = multiple(tmp, " strings");
        printf("%s\n", tmp);
        free(tmp);


Did You find this page useful?

Yes No



Last Modified: 12 December 2016

Releated Posts


2017-04-25 - Linux Programming - Signals the easy way
2017-03-01 - Shooting yourself in the head with threads
2013-02-16 - C - Some simple examples of using strcat
2012-11-03 - C - How to override malloc / free
2012-09-22 - Linux Programming - Using inotify for detecting file modifications
2012-08-09 - C++ - Check an IP Address is in a IP Mask
2012-04-05 - Using gdb to debug a core file
2012-03-15 - C - Is the stdin a tty
2012-03-13 - C - Converting from char / string to int using atoi
2012-03-08 - C - UDP Socket example
2012-02-20 - C - Get home dir location in linux
2012-02-17 - C - IP address validation
2012-02-15 - C - Get current ip address of an interface
2012-02-10 - Using asprintf instead of sprintf or snprintf
2012-01-31 - C - Example of how to overwrite argv
2012-01-30 - C - The string reverse
2012-01-27 - C - Palindrome
2012-01-26 - C - Example of using popen
2011-12-28 - C - gethostbyname example
2011-12-11 - C - Linux get mac address from interface