C Online Compiler
Example: String Concatenation Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Concatenation Example #include <stdio.h> #include <string.h> // Required for strcat() int main() { // Step 1: Declare a destination string with initial content and sufficient capacity // Ensure destination buffer is large enough for both strings + null terminator char greeting[30] = "Hello"; // Step 2: Declare a source string to append char name[] = " World!"; // Step 3: Concatenate name to greeting using strcat() strcat(greeting, name); // Step 4: Print the combined string printf("Combined string: %s\n", greeting); // Note: strcat also does not check buffer size, // potentially causing buffer overflow. For safer concatenation, use strncat(). return 0; }
Output
Clear
ADVERTISEMENTS