๐ŸŒ
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.
๐ŸŒ
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.
Discussions

c - What is the purpose of returning a char* in strcat, strcopy, (etc), when there is a destination variable? - Stack Overflow
Just a silly but quick question: Why do some functions that work with c style strings such as: fgets, strcpy, strcat, etc, have a return type of char* when there is a variable in the parameter lis... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - How to use strcat() function? - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Problem with strcat( ) function in c.
I'm a little rusty with C, but I believe the issue here is that if playerresponse is a string (I assume char*?), then playerresponse[2] is a char. strcat is expecting two char*, or two strings, basically. You should be able to use sprintf here: char sq[3]; sprintf(str, "%c%c", playerresponse[2], playerresponse[3]); More on reddit.com
๐ŸŒ r/learnprogramming
4
1
January 1, 2023
How does this pointer-based strcat work in C? - Stack Overflow
Since we // postfix-increment both ... value. return rdest; } Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)? A precondition for strcat is that dest ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ string.h โ€บ strcat
C strcat() - C Standard Library
The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.
๐ŸŒ
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.
๐ŸŒ
Sternum IoT
sternumiot.com โ€บ home โ€บ strcat function in c โ€“ syntax, examples, and security best practices
strcat C Function | Syntax, Examples, and Security Best Practices | Sternum IoT
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 [โ€ฆ]
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c strcat()
C strcat() - Scaler Topics
November 3, 2023 - The strcat() function which defined under the string.h header file in c concatenates two strings and returns a string in which the two strings are appended.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ string_h โ€บ strcat.php
C Language: strcat function (String Concatenation)
In the C Programming Language, the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.
๐ŸŒ
W3Resource
w3resource.com โ€บ c-programming โ€บ string โ€บ c-strcat.php
C strcat() function
3 weeks ago - C strcat() function (string.h): The strcat() function concatenates string2 to string1 and ends the resulting string with the null character.
Find elsewhere
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ string.h โ€บ strcat
C Programming/string.h/strcat - Wikibooks, open books for an open world
The name strcat is an abbreviation of "string concatenate". strcat is found in the string.h header file. ... char str1[14] = "Hello,"; /* The array has enough space for 'Hello,' plus " world!" plus a null terminator */ strcat(str1, " world!"); puts(str1); /* prints "Hello, world!" to stdout followed by a newline */ ... char * strcat(char *dest, const char *src) { size_t i,j; for (i = 0; dest[i] != '\0'; i++) ; for (j = 0; src[j] != '\0'; j++) dest[i+j] = src[j]; dest[i+j] = '\0'; return dest; }
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 11 โ€บ c-strcat-function-with-example
C strcat() Function with example
September 11, 2022 - It concatenates the specified string at the end of the another specified string. In this tutorial, we will see the strcat() function with example. ... This function takes two pointer as arguments and returns the pointer to the destination string after concatenation.
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ the-strcat-function-in-c
The strcat() Function in C - C Programming Tutorial - OverIQ.com
This function accepts two arguments ... first string is removed then second string is appended at the end of the first string. It returns a pointer to the resulting string (strg1). Generally, the return value of strcat() is discarded....
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;
}
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ c-in-a โ€บ 0596006977 โ€บ re215.html
strcat - C in a Nutshell [Book]
December 16, 2005 - The strcat() function copies the ... over the terminating null character of the string addressed by s1.The function returns the value ......
Authors ย  Peter PrinzTony Crawford
Published ย  2005
Pages ย  618
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ problem with strcat( ) function in c.
r/learnprogramming on Reddit: Problem with strcat( ) function in c.
January 1, 2023 -
char sq[10]=strcat(playeresponse[2],playerresponse[3]);
            printf("%s",sq);

Hello guys my compiler is throwing couple of errors. Can anyone help me?

playerresponse is a string.

So I want the array sq to be a string of two characters(playeresponse[2] and playeresponse[3])

Top answer
1 of 2
3
I'm a little rusty with C, but I believe the issue here is that if playerresponse is a string (I assume char*?), then playerresponse[2] is a char. strcat is expecting two char*, or two strings, basically. You should be able to use sprintf here: char sq[3]; sprintf(str, "%c%c", playerresponse[2], playerresponse[3]);
2 of 2
2
So I want the array sq to be a string of two characters(playeresponse[2] and playeresponse[3]) So why not just make that using aggregate initialisation? (sq is a terrible name BTW, what actually /is/ this thing?) char sq[3] = { playeresponse[2], playerresponse[3], 0 }; Of course, that's fine for individual characters. You should be using things like strcat what dealing with actual strings... char sq[10]=strcat(playeresponse[2],playerresponse[3]); But this isn't it. :) char sq[10] <-- is a variable declaration of an array of characters called sq strcat(playeresponse[2],playerresponse[3]); <-- is a bad call to strcat: the arguments are the value of those characters, not pointers to null-terminated strings ... and that's not how strcat 'works'. It doesn't make new strings, but appends (concatenates) one to another. Look at the documentation for strcat. It appends the null-terminated string pointed to by src to the end of the null-terminated string pointed to by dst. It requires that there be sufficient space in the array pointed to by dst to write the new characters from src... (and that's something you have to make sure is true!). The return value is just dst. We usually ignore the return value of strcat, it's something we already know... An example of using strcat: char a[42] = "batman"; // this is an array of 42 characters, containing a null-terminated string "batman". It has room for more things. const char* b = " smells"; // this is a pointer to a string literal which contains a null-terminated string " smells". This is a pointer to a non-modifiable array of characters containing the string. It cannot be modified or appended to etc. strcat(a, b); // this copies the characters from the string pointed to by b to the end of the string pointed to be a, writing into the latter parts of the array printf("%s\n", a); // prints "batman smells" and a newline
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstring โ€บ strcat
strcat - Concatenate strings
<cstring> strcat ยท function ยท <cstring> char * strcat ( char * destination, const char * source ); Concatenate strings ยท Appends a copy of the source string to the destination string.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ strcat-in-c-programming
C strcat function
1 month ago - In short, we can say the C strcat() ... joins two strings and stores the data in the first argument (destination) and returns its pointer....
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ c programming โ€บ strcat() function in c
strcat() Function in C
January 16, 2024 - The strcat() function in C programming language returns a pointer to the destination string, which is the same as the first parameter of the function.
Top answer
1 of 6
2
char * my_strcat(char *dest, const char *src)
{
    // Standard dictates strcat() to return dest.
    // That is pretty useless (returning a pointer to the
    // *end* of dest would have been better), but that's
    // the way it is.
    // Since we iterate dest as part of the implementation,
    // we need to "remember" its original value.
    char *rdest = dest;

    // Iterate over the characters pointed to by dest until
    // we found the end (null byte terminator), which is "false"    
    while (*dest)
      dest++;

    // An assignment evaluates to the value assigned. So assigning
    // one character at a time (*dest = *src) will eventually
    // evaluate to false when we assigned the null byte terminator
    // from src (incidentially also terminating dest). Since we
    // postfix-increment both pointers during the assignment, we
    // don't need any actual body for the loop.
    while (*dest++ = *src++)
      ;

    // Return the "remembered" original dest value.
    return rdest;
}

Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)?

A precondition for strcat is that dest must have enough space to hold the end result. So, no, it does not need / assign additional memory. It is up to you to make sure there is enough memory, or realloc more memory before you call strcat.

2 of 6
1

const char *src

src shouldn't be modified by the function, hence use const correctness to mark it as read-only.

char *rdest = dest;

Save the original position until later, since there's a requirement that strcat should return a pointer to the first element of the merged string (return rdest;).

while (*dest)
dest++;

The while loop is implicitly looking for the null terminator. Meaning: find the end of the first string, so that after this loop, dest points at the null terminator of that string.

while (*dest++ = *src++)

This is a common, although admittedly confusing idiom in C. (It actually implements strcpy in this line.) Operator precedence says postfix ++ takes precedence over prefix * over assignment =.

So first each pointer is evaluated and ++ is applied to the pointers, not the pointed-at data. But since it is postfix, the actual increment of the pointer address does not happen until the end of the expression.

* takes the contents of each pointer before this increment, and then = copies the content from *src to *dest. Again, this happens before the addresses are incremented.

Finally, there is an implicit check against null termination, since the result of the = operand can actually be checked - it is equivalent to its left operand, in this case *dest. And note that the null terminator gets copied, too.

You could rewrite this while loop in a less confusing way:

*dst = *src;
while(*src != '\0')
{
  dst++;
  src++;
  *dst = *src;
}
๐ŸŒ
Blogger
mybcaclassnotes.blogspot.com โ€บ p โ€บ c-strcat.html
The correct place for easy learning....: C strcat
The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.