Using strcat()/strcat_s() to append a string and strncat()/strncat_s() to append characters of a string

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Doing the string append using strcat() and strncat() C functions

To show: How to use C functions strcat()/strcat_s() and strncat()/strncat_s() to append a string and appending character of a string respectively

 

// Using strcat()/strcat_s()/wcscat()/_mbscat() and strncat()/strncat_s()/wcsncat()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char s2[50] = "New Year ";

char s1[50] = "Happy ";

char s3[50] = " ";

int count = 3;

 

printf("Using strcat()/strcat_s() and strncat()/strncat_s()\n");

printf("--------------------------------------------------------\n");

printf("s1 = %s\ns2 = %s\n", s1, s2);

// printf("\nstrcat (s1, s2) = %s\n", strcat(s1, s2)); - using a secure version

strcat_s(s1, 50, s2);

printf("\ns1 = %s, s2 = %s, s3 = %s\n", s1, s2, s3);

// strncat(s3, 50, s1); - using a secure version

strncat_s(s3, 50, s1, count);

printf("\ns1 = %s, s2 = %s, s3 = %s, count = %d\n", s1, s2, s3, count);

strcat_s(s3, 50, s2);

printf("\ns1 = %s, s2 = %s, s3 = %s\n", s1, s2, s3);

return 0;

}

 

Output example:

 

Using strcat()/strcat_s() and strncat()/strncat_s()

--------------------------------------------------------

s1 = Happy

s2 = New Year

s1 = Happy New Year , s2 = New Year , s3 =

s1 = Happy New Year , s2 = New Year , s3 = Hap, count = 3

s1 = Happy New Year , s2 = New Year , s3 = HapNew Year

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index