{"id":505,"date":"2019-05-04T18:02:13","date_gmt":"2019-05-04T18:02:13","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=505"},"modified":"2019-07-21T13:47:54","modified_gmt":"2019-07-21T13:47:54","slug":"define-variables-constants-using-modifiers","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/basic-syntax\/define-variables-constants-using-modifiers\/","title":{"rendered":"Variables and Constants"},"content":{"rendered":"\n<p>The first concept probably you need to know about any programming language is&nbsp;<strong>variable<\/strong>.&nbsp;<em>Variable<\/em>&nbsp;is a memory location identified by a memory address that store some quantity of data referred to as a&nbsp;<strong>value<\/strong>.<\/p>\n\n\n\n<p>You can define different types of variables based on the type of the data you plan to use in your application. <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/basic-syntax\/primitive-data-types\/\">Primitive data types<\/a> are the first variable types you need to learn in any programming language. <strong>Arrays<\/strong>, <strong>Enumerations<\/strong>, <strong>Data Structures<\/strong>,  <strong>Pointers<\/strong>, <strong>References<\/strong> and <strong>Classes<\/strong> are the other types of data type in C++.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Define a Variable<\/h1>\n\n\n\n<p>To define a variable, first you set <em>the type of the variable<\/em> and then you define the <em>variable name<\/em> or <em>list of variable names<\/em> which is separated by commas.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">type variable1, variable2, ...;<\/pre>\n\n\n\n<p>For examples:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int a, j, k;<br>char key;<br>long[] matrix;<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Define a Constant<\/h1>\n\n\n\n<p>To define a constant simply you can add <strong>const<\/strong> word before variable definition:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> const int DEFAULT_SIZE = 5;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream>\n\nconst int DEFAULT_SIZE = 5;\n\nint main() {\n std::cout &lt;&lt; \"Here is the value of DEFAULT_SIZE: \";\n std::cout &lt;&lt; DEFAULT_SIZE &lt;&lt; '\\n';\n\n std::cout &lt;&lt; \"You cannot change the value of a constant\" &lt;&lt; '\\n';\n\n std::cout &lt;&lt; \"But you can use its value in other expressions or as a function input.\" &lt;&lt; '\\n';\n int currentSize = 3 + DEFAULT_SIZE;\n std::cout &lt;&lt; \"For example: int currentSize = 3 + DEFAULT_SIZE: \";\n std::cout &lt;&lt; currentSize &lt;&lt; \"\\n\";\n\n   return 0;\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ g++ -o defconst defconst.cpp\n defconst.cpp:10:16: error: cannot assign to variable 'DEFAULT_SIZE' with const-qualified type 'const int'\n   DEFAULT_SIZE = 6;\n   ~~ ^ defconst.cpp:3:11: note: variable 'DEFAULT_SIZE' declared const here const int DEFAULT_SIZE = 5; ~~^~~\n 1 error generated.<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first concept probably you need to know about any programming language is&nbsp;variable.&nbsp;Variable&nbsp;is a memory location identified by a memory address that store some quantity of data referred to as a&nbsp;value. You can define different types of variables based on the type of the data you plan to use in your application. Primitive data types [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":499,"menu_order":2,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-505","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":24,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/505\/revisions"}],"predecessor-version":[{"id":592,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/505\/revisions\/592"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/499"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}