

{"id":120858,"date":"2024-03-27T18:00:55","date_gmt":"2024-03-27T12:30:55","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=120858"},"modified":"2024-03-27T18:14:20","modified_gmt":"2024-03-27T12:44:20","slug":"strcpy-vs-strcat-in-c","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/","title":{"rendered":"Difference Between strcpy() and strcat() Function in C"},"content":{"rendered":"<p>String manipulation is an essential concept in C programming. The C standard library provides functions like strcpy() and strcat() to copy and concatenate strings efficiently. In this post, we&#8217;ll examine how strcpy() and strcat() are used as well as some of their main distinctions.<\/p>\n<h2>The strcpy() Function in C<\/h2>\n<p>The strcpy() function inserts the whole string (including the null terminator) that is referenced by the source into the destination array.<\/p>\n<p><strong>The syntax is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char *strcpy(char *destination, const char *source);<\/pre>\n<p><strong>Let&#8217;s look at an example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main() {\r\n\r\n  char source[20] = \"Hello World\";\r\n  char destination[20];\r\n\r\n  strcpy(destination, source);\r\n\r\n  printf(\"Source string: %s\\n\", source);\r\n  printf(\"Destination string: %s\\n\", destination);\r\n  \r\n  return 0;\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Source string:<\/strong> Hello World<br \/>\n<strong>Destination string:<\/strong> Hello World<\/p>\n<p>Here, the source string &#8220;Hello World&#8221; is copied into the destination array using strcpy().<\/p>\n<p><strong>Some key points about strcpy():<\/strong><\/p>\n<ul>\n<li>Overwrites destination string fully even if arrays overlap<\/li>\n<li>Faster than strcat() in most cases<\/li>\n<li>Risk of buffer overflow if destination is too small<\/li>\n<\/ul>\n<h3>The strcat() Function in C<\/h3>\n<p>The strcat() function concatenates two strings by appending a copy of source to the end of destination.<\/p>\n<p><strong>The syntax is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char *strcat(char *destination, const char *source);<\/pre>\n<p><strong>Let&#8217;s look at an example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main() {\r\n\r\n  char str1[20] = \"Hello\";\r\n  char str2[20] = \"World\";\r\n\r\n  strcat(str1, str2);\r\n\r\n  printf(\"str1 before concat: %s\\n\", str1);\r\n  printf(\"str2 string: %s\\n\", str2);\r\n  printf(\"Concatenated string: %s\\n\", str1);\r\n\r\n  return 0;\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>str1 before concat:<\/strong> Hello<br \/>\n<strong>str2 string:<\/strong> World<br \/>\n<strong>Concatenated string:<\/strong> HelloWorld<\/p>\n<p>Here, strcat() appends str2 to str1, resulting in the concatenated string.<\/p>\n<p><strong>Key properties of strcat():<\/strong><\/p>\n<ul>\n<li>The destination must have a null terminator before concatenating<\/li>\n<li>Original destination value is not overwritten, only appended to<\/li>\n<li>Risk of buffer overflow if destination buffer is too small<\/li>\n<\/ul>\n<h3>Common Example<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main() {\r\n    char name1[10] = \"DataFlair\";\r\n    char name2[10] = \"Rohan\"; \/\/ Changed from \"John\" to \"Rohan\"\r\n    char name3[10];\r\n\r\n    strcpy(name3, name1);\r\n    printf(\"%s\\n\", name3); \/\/ Output: DataFlair\r\n\r\n    strcat(name1, name2);\r\n    printf(\"%s\\n\", name1); \/\/ Output: DataFlairRohan\r\n\r\n    return 0;\r\n}<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<li>Two character arrays, name1 and name2, are declared and initialized with &#8220;DataFlair&#8221; and &#8220;Rohan,&#8221; respectively.<\/li>\n<li>An empty character array name3 is declared.<\/li>\n<li>The strcpy function copies name1 to name3, resulting in name3 containing &#8220;DataFlair.&#8221;<\/li>\n<li>The printf function displays the content of name3 (&#8220;DataFlair&#8221;).<\/li>\n<li>The strcat function concatenates name2 to the end of name1, making name1 &#8220;DataFlairRohan.&#8221;<\/li>\n<li>The printf function displays the content of name1 (&#8220;DataFlairRohan&#8221;).<\/li>\n<li>The program exits with a status code of 0.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main() {\r\n\r\n  char source[100] = \"This is a source string\";\r\n  char dest[100];\r\n  \r\n  \/\/ Copy source to dest using strcpy()\r\n  strcpy(dest, source);\r\n  \r\n  \/\/ Concatenate additional string using strcat()\r\n  strcat(dest, \" - Extra appended text\"); \r\n  \r\n  printf(\"Source string: %s\\n\", source);\r\n  printf(\"Destination string: %s\\n\", dest);\r\n  \r\n  return 0;\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong>Source string:<\/strong> This is a source string<br \/>\n<strong>Destination string:<\/strong> This is a source string &#8211; Extra appended text<\/p>\n<p><strong>In this program:<\/strong><\/p>\n<ul>\n<li>strcpy() is used to copy the source string into destination string.<\/li>\n<li>strcat() is then used to append additional text to the destination string.<\/li>\n<li>Finally, both source and destination strings are printed to demonstrate the result.<\/li>\n<\/ul>\n<h3>When to Use Each Function<\/h3>\n<p><strong>Based on their behavior, here are some guidelines on when to use each one:<\/strong><\/p>\n<ul>\n<li>Use strcpy() when you need to completely replace the destination string<\/li>\n<li>Use strcat() when you need to append two strings by concatenating<\/li>\n<li>Use strcpy() in time-critical sections for better performance<\/li>\n<li>Use strcat() when maintaining null terminated substrings is required<\/li>\n<\/ul>\n<h3>Key Differences Between C strcpy() and strcat()<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Feature<\/b><\/td>\n<td><b>strcpy()<\/b><\/td>\n<td><b>strcat()<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Behavior<\/span><\/td>\n<td><span style=\"font-weight: 400\">Copies source string to destination<\/span><\/td>\n<td><span style=\"font-weight: 400\">Appends source string to destination<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Overwriting<\/span><\/td>\n<td><span style=\"font-weight: 400\">Overwrites destination fully<\/span><\/td>\n<td><span style=\"font-weight: 400\">Does not overwrite, only appends<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Null-termination<\/span><\/td>\n<td><span style=\"font-weight: 400\">Not required in the destination<\/span><\/td>\n<td><span style=\"font-weight: 400\">Required in destination before concatenating<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Return value<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns destination string<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns concatenated string<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Performance<\/span><\/td>\n<td><span style=\"font-weight: 400\">Faster<\/span><\/td>\n<td><span style=\"font-weight: 400\">Slower<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Use cases<\/span><\/td>\n<td><span style=\"font-weight: 400\">Complete replacement of destination needed<\/span><\/td>\n<td><span style=\"font-weight: 400\">Appending strings by concatenating<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Buffer issues<\/span><\/td>\n<td><span style=\"font-weight: 400\">Can cause overflow if destination is too small<\/span><\/td>\n<td><span style=\"font-weight: 400\">Can cause overflow if destination is too small after appending<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Conclusion<\/h3>\n<p>In summary, strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it. Keeping their exact behavior in mind helps us pick the right function and use it safely by managing null termination and buffers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String manipulation is an essential concept in C programming. The C standard library provides functions like strcpy() and strcat() to copy and concatenate strings efficiently. In this post, we&#8217;ll examine how strcpy() and strcat()&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":120861,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[23914,29931,29927,2263,29929,29930,29928],"class_list":["post-120858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-programming","tag-c-strcpy-and-strcat-function","tag-c-tutroials","tag-c","tag-difference-between-strcpy-and-strcat-function-in-c","tag-strcpy-and-strcat-function-in-c","tag-strcpy-vs-strcat-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Difference Between strcpy() and strcat() Function in C - DataFlair<\/title>\n<meta name=\"description\" content=\"strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference Between strcpy() and strcat() Function in C - DataFlair\" \/>\n<meta property=\"og:description\" content=\"strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-27T12:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-27T12:44:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference Between strcpy() and strcat() Function in C - DataFlair","description":"strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Difference Between strcpy() and strcat() Function in C - DataFlair","og_description":"strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it.","og_url":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-03-27T12:30:55+00:00","article_modified_time":"2024-03-27T12:44:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Difference Between strcpy() and strcat() Function in C","datePublished":"2024-03-27T12:30:55+00:00","dateModified":"2024-03-27T12:44:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/"},"wordCount":546,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp","keywords":["c programming","c strcpy() and strcat() function","c tutroials","C++","Difference Between strcpy() and strcat() Function in C","strcpy() and strcat() function in c","strcpy() vs strcat() in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/","url":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/","name":"Difference Between strcpy() and strcat() Function in C - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp","datePublished":"2024-03-27T12:30:55+00:00","dateModified":"2024-03-27T12:44:20+00:00","description":"strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/strcpy-vs-strcat-in-c.webp","width":1200,"height":628,"caption":"strcpy() vs strcat() in c"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/strcpy-vs-strcat-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"Difference Between strcpy() and strcat() Function in C"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=120858"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120858\/revisions"}],"predecessor-version":[{"id":131802,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120858\/revisions\/131802"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/120861"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=120858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=120858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=120858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}