C language strcat function - C language zero foundation tutorial

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

I Introduction to strcat function

In the previous article, the functions related to string copying are introduced, for example: strcpy function / strcpy_s function/ memcpy function / memcpy_s function Wait, today we will introduce a new one C language String processing function strcat, strcat function is mainly used for character string The syntax of this function is as follows:

/*
*Description: this kind of function is used to splice strings and connect two strings together
*
*Parameters:
*   [in] strSource: String to append
*   [out] strDestination: Target string
*
*Return value: pointer to the spliced string
*/

//Header file: string h

char* strcat(char* strDestination, const char* strSource);

1.strcat function Append the string pointed to by strSource to the end of the string pointed to by strDestination, so you must ensure that strDestination has enough memory space to accommodate two strings, otherwise overflow errors will be caused.

** 2.** The \ 0 at the end of strdestination will be overwritten, and the \ 0 at the end of strSource will be copied together. The final string has only one \ 0;

3. If the strcat function prompts error: 4996, please refer to: error C4996: 'fopen': This function or variable may be unsafe

error C4996: 'strcat': This function or variable may be unsafe.
Consider using strcat_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.

II Principle of strcat function

Principle of strcat function: dst memory space size = target string length + original string field + '\ 0';

Get memory space usage sizeof function (get memory space size); get string length using strlen function (check string length)

III strcat function practice

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com com
//@File:C language tutorial - C language strcat function
//@Time:2021/06/06 08:00
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/

#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "windows.h"
//error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#pragma warning( disable : 4996)
void main()
{
    char src[1024] = { "C/C++course-strcat function" };
    char dst[1024] = { "www.codersrc.com"};
    printf("strcat before dst:%s\n", dst); //Empty string
    strcat(dst, src);
    printf("strcat after dst:%s\n", dst);//
    system("pause");
}
/*
Output results:
strcat Previously DST: www.codersrc com
strcat Then DST: www.codersrc Comc / C + + tutorial - strcat function
 Please press any key to continue
*/

IV Note the strcat function crash

char src[1024] = { "C/C++course-strcat function" };
char dst[5] = { "www"};

printf("strcat before dst:%s\n", dst); //
strcat(dst, src); //dst has only 5 bytes. If src is spliced to the tail of dst, the space of dst cannot store all characters of src, overflow and crash
printf("strcat after dst:%s\n", dst);

V Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 uninstall
  5. Set Visual Studio font / background / line number
  6. C language format controller / placeholder
  7. C language logical operators
  8. C language ternary operator
  9. C language comma expression
  10. C language self addition and self subtraction operator (+ + i / i + +)
  11. C language for loop
  12. C language break and continue
  13. C language while loop
  14. C language do while and while loop
  15. C language switch statement
  16. C language goto statement
  17. C language char string
  18. C language strlen function
  19. sizeof function in C language
  20. Differences between sizeof and strlen functions in C language
  21. C language strcpy function
  22. C language strcpy_s function
  23. C language strcpy and strcpy_s function difference
  24. C language memcpy and memcpy_s difference
  25. C language strcat function

No reprint without permission: Ape programming ยป C language strcat function

Keywords: C

Added by djjamiegee on Sat, 25 Dec 2021 22:36:23 +0200