catalogue
- I Introduction to strcat function
- II Principle of strcat function
- III strcat function practice
- IV Note the strcat function crash
- V Guess you like it
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
- Install Visual Studio
- Install the Visual Studio plug-in Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 uninstall
- Set Visual Studio font / background / line number
- C language format controller / placeholder
- C language logical operators
- C language ternary operator
- C language comma expression
- C language self addition and self subtraction operator (+ + i / i + +)
- C language for loop
- C language break and continue
- C language while loop
- C language do while and while loop
- C language switch statement
- C language goto statement
- C language char string
- C language strlen function
- sizeof function in C language
- Differences between sizeof and strlen functions in C language
- C language strcpy function
- C language strcpy_s function
- C language strcpy and strcpy_s function difference
- C language memcpy and memcpy_s difference
- C language strcat function
No reprint without permission: Ape programming ยป C language strcat function