You can concatenate constant/literal strings in C by:
Directly placing them together: When two string literals are placed adjacent to each other, the compiler automatically concatenates them into a single string. For example:
C
const char* message = “Hello” “World”;
This will create a single string HelloWorld.
Using the sprintf() function: This function formats and prints data to a character array. You can use it to concatenate strings by specifying the format specifier %s for each string. For example:
C
char result[50];
sprintf(result, “%s%s”, “Hello”, “World”);
This will store the concatenated string “HelloWorld” in the result array.
Using the strcat() function: This function appends a copy of one string to the end of another string. However, you must ensure that the destination string has enough space to accommodate the concatenated result. For example:
C
char result[50] = “Hello”;
strcat(result, “World”);
This will modify the result array to contain “HelloWorld”.
Remember that string literals in C are immutable, meaning their contents cannot be changed after they are created. Therefore, methods like direct modification of the string pointer are not allowed.