{"id":61,"date":"2026-05-19T14:48:29","date_gmt":"2026-05-19T14:48:29","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=61"},"modified":"2026-05-22T06:18:43","modified_gmt":"2026-05-22T06:18:43","slug":"variables-and-constants","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=variables-and-constants","title":{"rendered":"Variables and Constants"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Variables and constants are basic building blocks of C++ programming. They are used to store data that a program can use and process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Variable?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A variable is a named memory location used to store data. The value of a variable can change during program execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Variable<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>data_type variable_name;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int age;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing a Variable<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can assign a value to a variable during declaration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 18;<\/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>    int age = 18;<br><br>    cout &lt;&lt; age;<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>18<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Variables<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Integer Variable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stores whole numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int number = 10;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Float Variable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stores decimal numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float price = 99.5;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Character Variable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stores single characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char grade = 'A';<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">String Variable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stores text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string name = \"Ali\";<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Rules for Naming Variables<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Names can contain letters, digits, and underscore<\/li>\n\n\n\n<li>Names cannot start with a number<\/li>\n\n\n\n<li>Spaces are not allowed<\/li>\n\n\n\n<li>C++ keywords cannot be used<\/li>\n\n\n\n<li>Variable names are case-sensitive<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Valid examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int age;<br>float total_marks;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Invalid examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int 2age;<br>float total marks;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Constant?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constant is a value that cannot be changed during program execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using const Keyword<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const float PI = 3.14;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Constant<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    const int DAYS = 7;<br><br>    cout &lt;&lt; DAYS;<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>7<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Variable and Constant<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Variable<\/th><th>Constant<\/th><\/tr><\/thead><tbody><tr><td>Value can change<\/td><td>Value cannot change<\/td><\/tr><tr><td>Declared normally<\/td><td>Declared using <code>const<\/code><\/td><\/tr><tr><td>Used for changing data<\/td><td>Used for fixed values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why Variables and Constants are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">They are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store program data<\/li>\n\n\n\n<li>Help perform calculations<\/li>\n\n\n\n<li>Make programs flexible<\/li>\n\n\n\n<li>Improve code readability<\/li>\n\n\n\n<li>Manage fixed and changing values efficiently<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779430703370\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Variable \u2192 Bank balance that changes<\/li>\n\n\n\n<li>Constant \u2192 Number of days in a week<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variables and constants are essential concepts in C++ programming. Variables store changing data, while constants store fixed values. Understanding them helps build a strong foundation for programming and problem-solving.<\/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\">C++ Fundamentals (Beginner Level) > Basics of Programming > Variables and Constants<\/span><\/span><\/div>","protected":false},"menu_order":7,"template":"","class_list":["post-61","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>Variables and Constants - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.\" \/>\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=\"Variables and Constants - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.\" \/>\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-22T06:18:43+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=variables-and-constants\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Variables and Constants - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-19T14:48:29+00:00\",\"dateModified\":\"2026-05-22T06:18:43+00:00\",\"description\":\"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.\",\"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\":\"C++ Fundamentals (Beginner Level) > Basics of Programming > Variables and Constants\"}]},{\"@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":"Variables and Constants - Learn C++Language with GiGz.PK","description":"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.","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":"Variables and Constants - Learn C++Language with GiGz.PK","og_description":"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T06:18:43+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=variables-and-constants","url":"https:\/\/gigz.pk\/","name":"Variables and Constants - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-19T14:48:29+00:00","dateModified":"2026-05-22T06:18:43+00:00","description":"Learn C++ variables and constants including data storage, naming rules, data types, and const keyword with examples.","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":"C++ Fundamentals (Beginner Level) > Basics of Programming > Variables and Constants"}]},{"@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\/61","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=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}