Both of the code snippet invoke undefined behavior. In the first snippet s1 has not enough space to hold any other character than six characters.
In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.

Read strcat man page:

[...] The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

Answer from haccks on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcat-in-c
strcat() in C - GeeksforGeeks
March 11, 2023 - C strcat() function appends the string pointed to by src to the end of the string pointed to by dest. It will append a copy of the source string in the destination string. plus a terminating Null character.
🌐
W3Schools
w3schools.com › c › ref_string_strcat.php
C string strcat() Function
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... The strcat() function appends a copy of one string to the end of another.
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcat
C strcat() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char str1[100] = "This is ", str2[] = "programiz.com"; // concatenates str1 and str2 // the resultant string is stored in str1. strcat(str1, str2); puts(str1); puts(str2); return 0; }
Top answer
1 of 2
10

Both of the code snippet invoke undefined behavior. In the first snippet s1 has not enough space to hold any other character than six characters.
In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.

Read strcat man page:

[...] The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

2 of 2
7

In C the function strcat does not create a new character array containing concatenated strings. It appends characters from the second string to the first string of the first character array provided that it has enough elements to store the new characters. Otherwise the function will try to overwrite the memory beyond the character array that results in undefined behavior.

So a valid use of the function in the first program can look the following way

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

int main(int argc, const char *argv[]) {
    char s1[11] = "12345";
    char s2[] = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
} 

In this program the character array is declared as having 11 elements. Thus it is able to accommodate the appended string "abcde".

In the second program there is an attempt to modify the string literal pointed to by the pointer s1. String literals in C and C++ are immutable. Any attempt to change a string literal results in undefined behavior even though in C opposite to C++ string literals have types of non-constant character arrays.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

So in the second program you again need to use a character array with enough elements. For example

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

int main(int argc, const char *argv[]) {
    char s1[11] = "12345";
    char* s2 = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

Or you could use either a Variable Length Array (VLA) if the compiler supports them or dynamically allocate an array. For example

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

int main(int argc, const char *argv[]) {
    char *s1 = "12345";
    char* s2 = "abcde";
    char s3[strlen( s1 ) + strlen( s2 ) + 1];    

    strcpy( s3, s1 );
    strcat( s3, s2 );

    puts(s1);
    puts(s2);
    puts(s3);

    return 0;
}

Or

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

int main(int argc, const char *argv[]) {
    char *s1 = "12345";
    char* s2 = "abcde";
    char *s3 = malloc( strlen( s1 ) + strlen( s2 ) + 1 );    

    if ( s3 != NULL )
    {
        strcpy( s3, s1 );
        strcat( s3, s2 );

        puts(s1);
        puts(s2);
        puts(s3);
    }

    free( s3 );

    return 0;
}
🌐
Dartmouth College
cs.dartmouth.edu › ~campbell › cs50 › strcat.c
https://www.cs.dartmouth.edu/~campbell/cs50/strcat.c
#define SIZE 80 int main() { char str[SIZE]; strcpy(str,"Professor "); strcat(str,"Andrew "); strcat(str,"Thomas "); strcat(str,"Campbell"); puts(str); return EXIT_SUCCESS; }
🌐
Sternum IoT
sternumiot.com › home › strcat function in c – syntax, examples, and security best practices
strcat Function in C – Syntax, Examples, and Security Best ...
August 2, 2023 - It is used for string concatenation, which is the process of appending one string to the end of another string. Its name is a contraction of “string concatenate”. The function syntax is: char *strcat(char […]
Find elsewhere
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009696799 › functions › strcat.html
strcat
The strcat() function shall append a copy of the string pointed to by s2 (including the terminating null byte) to the end of the string pointed to by s1. The initial byte of s2 overwrites the null byte at the end of s1.
🌐
Vultr
docs.vultr.com › clang › standard-library › string-h › strcat
C string.h strcat() - Concatenate Strings | Vultr Docs
September 27, 2024 - The strcat() function is a standard library function in C, found within the <string.h> header, used to concatenate two strings. This function appends the content of one string to another, effectively joining them into a single string.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strcat.htm
C library - strcat() function
The C Library strcat() function accepts two pointer variable as parameters(say dest, src) and, appends the string pointed to by src to the end of the string pointed to by dest. This function only concatenates the string data type.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › strcat() in c
strcat() in C | Parameters and Top 8 Examples of strcat() in C
March 23, 2023 - The strcat() function in c is usually used for the string concatenation in the programming process. strcat() concatenates a specific string with another string. It is a built-in function.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Medium
medium.com › @katongoleisaac › my-implementation-of-c-strcat-using-pointer-97a7250df3d9
My Implementation of C strcat using pointer | by Isaac | Medium
May 11, 2025 - strcat — This is function from standard C library (string.h) that concatenates two character strings. /** * Implmentation of strcat using a pointer * */ #include <stdio.h> #include <string.h> #include <stdlib.h> char *strcatt(const char *s, ...
Top answer
1 of 7
20

Change

char *message = "\n\nHTTP/1.1 ";

to

char message[1024];  
strcpy(message,"\n\nHTTP/1.1 ");

and you should be ok, up to a total message length of 1023.

Edit: (as per mjy's comment). Using strcat in this fashion is a great way of getting buffer overflows. You could readily write a small function that checks the size of the buffer and length of incoming string addition to overcome this, or use realloc on a dynamic buffer. IMO, the onus is on the programmer to check correct buffer sizes where they are used, as with sprintfs and other C strings functions. I assume that C is being used over C++ for performance reasons, and hence STL is not an option.

Edit: As per request from Filip's comment, a simple strcat implementation based on a fixed size char buffer:

char buffer[MAXSIZE] = "";

int mystrcat(char *addition)
{
   if (strlen(buffer) + strlen(addition) + sizeof(char)  >= MaxSize)
     return(FAILED);
   strcat(buffer,addition);
   return(OK);
}

Using dynamic allocation:

char *buffer = NULL;

int mystrcat(char *addition)
{
   buffer = realloc(buffer, strlen(buffer) + strlen(addition) + sizeof(char));
   if (!buffer)
     return(FAIL);
   strcat(buffer, addition);
   return(OK);
}

In this case you have to free your buffer manually when you are finished with it. (Handled by destructors in C++ equivalents)

Addendum (Pax):

Okay, since you didn't actually explain why you had to create message[1024], here it is.

With char *x = "hello", the actual bytes ('h','e','l','l','o',0) (null on the end) are stored in an area of memory separate from the variables (and quite possibly read-only) and the variable x is set to point to it. After the null, there's probably something else very important. So you can't append to that at all.

With char x[1024]; strcpy(x,"hello");, you first allocate 1K om memory which is totally dedicated to x. Then you copy "hello" into it, and still leave quite a bit of space at the end for appending more strings. You won't get into trouble until you append more than the 1K-odd allowed.

End addendum (Pax):

2 of 7
8

I wonder why no one mentioned snprintf() from stdio.h yet. That's the C way to output multiple values and you won't even have to convert your primitives to strings beforehand.

The following example uses a stack allocated fixed-sized buffer. Otherwise, you have to malloc() the buffer (and store its size), which would make it possible to realloc() on overflow...

char buffer[1024];
int len = snprintf(buffer, sizeof(buffer), "%s %i", "a string", 5);
if(len < 0 || len >= sizeof(buffer))
{
    // buffer too small or error
}

Edit: You might also consider using the asprintf() function. It's a widely available GNU extension and part of TR 24731-2 (which means it might make it into the next C standard). The example from above would read

char * buffer;
if(asprintf(&buffer, "%s %i", "a string", 5) < 0)
{
    // (allocation?) error
}

Remember to free() the buffer when done using it!

🌐
Cplusplus
cplusplus.com › reference › cstring › strcat
strcat - Concatenate strings
char * strcat ( char * destination, const char * source ); Concatenate strings · Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the ...
🌐
Weber State University
icarus.cs.weber.edu › ~dab › cs1410 › textbook › 8.Strings › strcat.html
8.2.2.3. strcat
The strcat function. The string-concatenate function concatenates two C-strings by appending its second argument to its first. The program passes both arguments to the function by pointer, but the second argument is const and can't be modified. The standard and Microsoft versions return the ...
🌐
Decodejava
decodejava.com › c-strcat-function.htm
C - strcat() function - Decodejava.com
char * strcat ( char * destination, const char * source ) A source is a string passed to this function, whose characters are to be appended at the end of another string destination. This function appends the character of source string at the end of the characters present in the destination string and returns a char[] array pointer, pointing to the destination string containing the addition of both string values.
🌐
cppreference.com
en.cppreference.com › c › string › byte › strcat
strcat, strcat_s - cppreference.com
November 14, 2020 - The behavior is undefined if the size of the character array pointed to by dest < strlen(dest)+strlen(src)+1 <= destsz; in other words, an erroneous value of destsz does not expose the impending buffer overflow. As with all bounds-checked functions, strcat_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <string.h>.
🌐
ZetCode
zetcode.com › clang › strcat
C strcat Tutorial: String Concatenation with Practical Examples
April 8, 2025 - The strcat function concatenates (appends) one string to another. It's declared in string.h and takes two parameters: the destination string and source string. strcat appends the source string to the destination string, overwriting its null terminator. The destination buffer must have enough ...
🌐
Linux Man Pages
linux.die.net › man › 3 › strcat
strcat(3): concatenate two strings - Linux man page
The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.