EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

strcat() in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » strcat() in C

strcat() in C

Introduction to strcat() in C

The strcat() function in c is usually used for the string concatenation in the programming process. strcat() concatenates a specific string with another string. It is a built-in function. Strcat() built-in function in c will only work without any error if you define <string.h> in the header file/position of the Project/Program.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Char *strcat(char *str1, const char *str2)

Parameters:

The above built-in function ( strcat() ) has/takes just 2 arguments which may be two strings/the character arrays. Strings that are concatenated will be stored in the first string itself in the argument.

  • Str1: destination string’s pointer.
  • Str2: source string’s pointer which can be appended/added to the destination string.

Examples to Implement strcat() in C

Below are the different examples of strcat() in C programming:

Example #1

Program to concatenate the strings. Here stra1, stra2 already assigned with the string values by assigning.

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char stra1[] = "This is ", stra2[] = "programiz.com";
//concatenates stra1 and stra2 and resultant string is stored in str1.
strcat(stra1,stra2);
puts(stra1);
puts(stra2);
return 0;
}

Output:

strcat() in C eg1

Example #2

Program to concatenate stra1 and stra2 string’s variable values.

Code:

#include <stdio.h>
#include <string.h>
int main () {
char stra1[1000], stra2[1000];
//stra1 destination string
strcpy(stra1, "Hi I m Pavan Kumar Sake . This is my 1st string to append to other string , \n");
//stra2 source string
strcpy(stra2, " (This is my 2nd string to concatenate and then to print the result from the 1st string variable )\n" );
//concatenating the string stra2 to the string stra1
strcat(stra1, stra2);
//stra1 : displaying destination string
printf("String after concatenation: %s", stra1);
return(0);
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

strcat() in C eg2

Example #3

Program of Concatenation after copying the specific string to the stra1, stra2.

Code:

#include <stdio.h>
#include <string.h>
int main () {
char stra1[1000], stra2[1000];
//stra1 destination string
strcpy(stra1, "Hi I m Pavan Kumar Sake . This is my 1st string \n to append to other string , ");
//stra2 source string
strcpy(stra2, "(This is my 2nd string to concatenate and then to print \n the result from the 1st string variable )" );
//concatenating the string stra2 to the string stra1 in the printf statement itself
printf("String after concatenation: %s", strcat(stra1, stra2));
//The resultant of the concatenated string usually stores in stra1 but here strcat() used in the print statement itself
//so no need to declare stra1 again
return(0);
}

Output:

strcat() in C eg3

Same output as the above example’s output due to the same programming logic except for the declaration of the strcat() position. The same strings are used just like the above.

Example #4

Program of concatenation in the print statement itself in C Programming Language.

Code:

#include <stdio.h>
#include <string.h>
int main () {
//Assigning strings to the variables
char stra1[1000] = "Hello I m Pavan Kumar Sake . This is my 1st string to append to other string , \n";
char stra2[1000] = " (This is my 2nd string to concatenate and then to print the result from the 1st string variable )";
//concatenating the string stra2 to the string stra1 in the printf statement itself
printf("String after concatenation: %s", strcat(stra1, stra2));
//The resultant of the concatenated string usually stores in stra1 but here strcat() used in the print statement itself
//so no need to declare stra1 again
return(0);
}

Output:

strcat() in C eg4

Example #5

C Program to show concatenation of 2 strings using 2 different types i.e., normal & also by shifting the source and the target strings. Check the c program below, you will know.

Code:

#include <stdio.h>
#include <string.h>
int main( )
{
char source1[ ] = " Best one" ;
//assigning the string to the source1 string
char target1[ ]= " Java tutorial" ;
//assigning the string to the target1 string
printf ( "\nSource string 1st one = %s ", source1 ) ;
//printing the source1 value
printf ( "\nTarget string 2nd one = %s \n", target1 ) ;
//printing the target value
strcat ( target1, source1 ) ;
//concatenating the target1 and the source1 values/strings
printf ( "\n Target string after strcat( ) = %s \n ", target1 ) ;
//target1 will now have the concatenated and then it will be printed
printf ( "\n Source string after strcat( ) = %s \n ", strcat(source1, target1) ) ;
//source 1 doesnot have any value because already string appended and the resultant stored in the target string/chars
}

Output:

strcat() in C eg5

Example #6

Program of concatenation using the define variables and with the NULL CHARACTER.

Code:

#include <stdio.h>
#include <string.h>
//Program to Concatenate the NULL STRING
#define DEST_SIZE1 40
// defining the dest_size1 of 40
char dest1[DEST_SIZE1];
//assigning the dest with dest size
int main()
{
strcat(dest1, "Look Here");
//concatenating the null string/character
printf(dest1);
//Printing the dest1 value
return 0;
}

Output:

define variables

Example #7

Program of concatenation using Pointers.

Code:

#include <stdio.h>
#include <string.h>
//String Character Program with the pointers used
#define DEST_SIZE1 40
int main()
{
char src1[] = "Look Here";
//assigning string to the src1 variable
char dest1[DEST_SIZE1] = "Unimaginable";
//assigning string to the dest1 variable
char *ps1 = src1 + 4;
//assigning src1 string variables value to the pointer ps1
char *pd1 = dest1 + 6;
//assigning dest1 string variables value to the pointer pd1
strcat(pd1, ps1);
//concatenating the pointers
printf(dest1);
//printing dest1 value
strcat(src1, dest1);
//concatenating the src1 value by concatenating the src1 and dest1
printf("\n\n");
//line breaks
printf(src1);
//printing src1 value - it is the concatenation value
return 0;
}

Output:

concatenation using Pointers output

Example #8

This is the program of strcat()’s similar function strncat(). It is very helpful to concatenate only the particular length of the characters from the string.

Code:

#include <stdio.h>
#include <string.h>
//This is a program of strncat() function to concatenate only first 4 characters of the string.
#define DEST_SIZE1 40
//defining the dest_size1 with 40 value
int main()
{
char src1[] = " Hey Buddy!!! ";
//assigning the string to the src character's string - 1st string
char dest1[DEST_SIZE1] = "How are you";
//assigning the string value to the dest[40] because dest_size1 already assigned - 2nd string
strncat(dest1, src1, 4);
//concatenating the dest, src with the condition of concatenating only 4 strings of src1
printf(dest1);
//printing the dest1 value because now the dest1 now has concatenated string
return 0;
}

Output:

Output

Conclusion

This is about the concept of strcat() in C Programming Language and how strcat() function should be declared, strcat() works in C i.e., how strcat() function will append/ concatenate the 2 strings. All outputs with some examples of strcat are listed above.

Recommended Articles

This is a guide to strcat() in C. Here we discuss the syntax, Parameters and top examples of strcat() in C along with code implementation. You may also look at the following articles to learn more-

  1. Examples of C Union
  2. 2-D Arrays in C
  3. Decimal to Octal in C
  4. Function Prototype in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More