{"id":101,"date":"2026-05-20T11:39:20","date_gmt":"2026-05-20T11:39:20","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=101"},"modified":"2026-05-22T10:25:20","modified_gmt":"2026-05-22T10:25:20","slug":"character-arrays","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=character-arrays","title":{"rendered":"Character Arrays"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Character arrays in C++ are arrays used to store multiple characters together. They are commonly used to store words and sentences as strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Character Array?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A character array is an array of characters (<code>char<\/code>) where each element stores a single character.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Character arrays are often used to represent strings in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Character Arrays?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Character arrays are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store text data<\/li>\n\n\n\n<li>Handle words and sentences<\/li>\n\n\n\n<li>Work efficiently with strings<\/li>\n\n\n\n<li>Are widely used in programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Character Array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>char array_name&#91;size];<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Declaration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>char name&#91;10];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This array can store up to 9 characters plus a null character (<code>\\0<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing Character Array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>char name&#91;] = \"Ali\";<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Strings Work in Character Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A string in C++ ends with a special null character:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'A' 'l' 'i' '\\0'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Program<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    char name&#91;] = \"Ahmed\";<br><br>    cout &lt;&lt; name;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ahmed<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Individual Characters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each character has an index number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    char name&#91;] = \"Ali\";<br><br>    cout &lt;&lt; name&#91;0] &lt;&lt; endl;<br>    cout &lt;&lt; name&#91;1] &lt;&lt; endl;<br>    cout &lt;&lt; name&#91;2] &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>A<br>l<br>i<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Taking Input in Character Array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    char name&#91;20];<br><br>    cin &gt;&gt; name;<br><br>    cout &lt;&lt; name;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using getline for Full Sentence<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    char sentence&#91;50];<br><br>    cin.getline(sentence, 50);<br><br>    cout &lt;&lt; sentence;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Character Array Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Character arrays can work with functions from the <code>&lt;cstring&gt;<\/code> library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>strlen()<\/code> \u2192 Find length<\/li>\n\n\n\n<li><code>strcpy()<\/code> \u2192 Copy string<\/li>\n\n\n\n<li><code>strcat()<\/code> \u2192 Join strings<\/li>\n\n\n\n<li><code>strcmp()<\/code> \u2192 Compare strings<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of strlen()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;cstring&gt;<br>using namespace std;<br><br>int main() {<br><br>    char name&#91;] = \"Cplusplus\";<br><br>    cout &lt;&lt; strlen(name);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Character Arrays<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Efficient memory usage<\/li>\n\n\n\n<li>Fast string processing<\/li>\n\n\n\n<li>Useful in low-level programming<\/li>\n\n\n\n<li>Compatible with C language functions<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Fixed size<\/li>\n\n\n\n<li>Manual memory handling<\/li>\n\n\n\n<li>Less safe than modern string class<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Character Arrays are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Character arrays are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Form the base of string handling<\/li>\n\n\n\n<li>Are used in system programming<\/li>\n\n\n\n<li>Help understand memory concepts<\/li>\n\n\n\n<li>Are widely used in C and C++<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a word written letter by letter in separate boxes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each box stores one character<\/li>\n\n\n\n<li>Together they form a complete word<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how character arrays work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Character arrays in C++ are essential for storing and managing text data. They provide a basic and efficient way to work with strings and are an important concept for understanding string manipulation and memory handling in programming.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Arrays and Strings > Character Arrays<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":27,"template":"","class_list":["post-101","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Character Arrays - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Character Arrays - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:25:20+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=character-arrays\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Character Arrays - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:39:20+00:00\",\"dateModified\":\"2026-05-22T10:25:20+00:00\",\"description\":\"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Arrays and Strings > Character Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Character Arrays - Learn C++Language with GiGz.PK","description":"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Character Arrays - Learn C++Language with GiGz.PK","og_description":"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:25:20+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=character-arrays","url":"https:\/\/gigz.pk\/","name":"Character Arrays - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:39:20+00:00","dateModified":"2026-05-22T10:25:20+00:00","description":"Learn C++ character arrays with examples to store and handle strings, text input, and common string functions like strlen and strcpy.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Arrays and Strings > Character Arrays"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}