C program for String Concatenation using strcat function

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. 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
The strcat() and strncat() functions return a pointer to the resulting string dest.

The declaration of strcat looks like,

 $ char *strcat(char *dest, const char *src); 
 $ vim strcat_test.c 
#include <stdio.h>
#include <string.h>

int main(void) {
        char str[128]="hello";
        printf("First string: \"%s\" : length:%d \n", str, strlen(str));
        printf("String to append: \"%s\" : length:%d \n", "world", strlen("world"));
        printf("Concatnated string: %s \n", strcat(str, "world"));

        printf("strlen of final string = %d\n", strlen(str));
        return 0;
}
 $ $ gcc -o strcat_test strcat_test.c 
 $ ./strcat_test

First string: "hello" : length:5 
String to append: "world" : length:5 
Concatnated string: helloworld 
strlen of final string = 10

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply