
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strcat() Function in C language?
The C library function char *strcat(char *dest, const char *src) appends the string pointed to by src to the end of the string pointed to by dest.
An array of characters is called a string.
Declaration
Following is the declaration for an array −
char stringname [size];
For example − char string[50]; string of length 50 characters
Initialization
- Using single character constant −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char string[10] = "Hello":;
Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.
The strcat( ) function
This is used for combining or concatenating two strings.
The length of the destination string must be greater than the source string.
The result concatenated string is the source string.
Syntax
The syntax is as follows −
strcat (Destination String, Source string);
Example Program
The following program shows the usage of strcat() function.
#include <string.h> main(){ char a[50] = "Hello
"; char b[20] = "Good Morning
"; strcat (a,b); printf("concatenated string = %s", a); }
Output
When the above program is executed, it produces the following result −
Concatenated string = Hello Good Morning
Example
Let’s see another example.
Following is the C program to concatenate source string to destination string using strcat library function −
#include<stdio.h> #include<string.h> void main(){ //Declaring source and destination strings// char source[45],destination[50]; //Reading source string and destination string from user// printf("Enter the source string :
"); gets(source); printf("Enter the destination string :
"); gets(destination); //Concatenate all the above results// strcat(source,destination); //Printing destination string// printf("The modified destination string :"); puts(source); }
Output
When the above program is executed, it produces the following result −
Enter the source string :Tutorials Point Enter the destination string :C programming The modified destination string :Tutorials Point C programming
- Related Articles
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok() function in C language?
- What is strtok_r() function in C language?
- What is a malloc function in C language?
