{"id":39,"date":"2019-04-14T21:01:09","date_gmt":"2019-04-14T21:01:09","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=39"},"modified":"2020-08-06T09:52:00","modified_gmt":"2020-08-06T09:52:00","slug":"c-variable-declaration-and-definition","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-variable-declaration-and-definition\/","title":{"rendered":"C++ Variable Declaration and Definition"},"content":{"rendered":"<p>We already know that data is stored in memory. So variables allow you to name the storage for your data. Therefore, you as a programmer can create variables.<\/p>\n<p>Each variable must have a specific data type. (<a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-types\/\">you can review data types<\/a>). The data type determines the size of the variable. That is the size of memory it occupies.<\/p>\n<p>To name a variable, there are some rule to follow:<\/p>\n<ul>\n<li>variable name must begin with a letter or an underscore<\/li>\n<li>cannot begin with a number or special character<\/li>\n<li>names are case-sensitive<\/li>\n<\/ul>\n<p>Examples of variables are: score, student, num1, _var<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Variable Types<\/strong><\/p>\n<p>This is about the same as <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-types\/\">Data Types<\/a>. The basic types of variables in C++ are listed below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>S.No<\/th>\n<th>Type and brief Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>bool<\/b><\/p>\n<p>Stores either value true or false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>char<\/b><\/p>\n<p>Typically a single octet (one byte). This is an integer type.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>int<\/b><\/p>\n<p>The most natural size of integer for the machine.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>float<\/b><\/p>\n<p>A single-precision floating point value.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>double<\/b><\/p>\n<p>A double-precision floating point value.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>void<\/b><\/p>\n<p>Represents the absence of type.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>wchar_t<\/b><\/p>\n<p>A wide character type.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Now, the types in the list above is also known as primitive types. However, C++ also provides other types of variables. These include:<\/p>\n<ul>\n<li>enum<\/li>\n<li>Pointer<\/li>\n<li>Array<\/li>\n<li>Reference<\/li>\n<li>Collections<\/li>\n<li>Classes<\/li>\n<\/ul>\n<p>We would examine these in subsequent lessons<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Defining and Initializing a Variable<\/strong><\/h4>\n<p>Before you can use a variable, you must define it. This simply means you provide a name of the variable, then you provide the data type as well. In this way, the compiler reserves the right amount of space for the variable.<\/p>\n<p>Examples of how to define a variable is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> a, b c; <span style=\"color: #888888;\">\/\/declares the variables a, b, c as int<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">float<\/span> score; <span style=\"color: #888888;\">\/\/declares the variables score as float<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">char<\/span> grade; <span style=\"color: #888888;\">\/\/declares the variables grade as char<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> amount, salary; <span style=\"color: #888888;\">\/\/declares the variables amount and salary as double<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Initializing a Variable<\/strong><\/p>\n<p>This means assigning an initial value to the variable. This can be done along with the declaration. It could also be done\u00a0 in another line. The code below declares and initialized the same variables.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> a <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, b <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, c <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>; \r\n<span style=\"color: #333399; font-weight: bold;\">float<\/span> score <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">99.3<\/span>; \r\n<span style=\"color: #333399; font-weight: bold;\">char<\/span> grade  <span style=\"color: #0044dd;\">'A'<\/span>; \r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> amount <span style=\"color: #0000dd; font-weight: bold;\">8000<\/span>, salary <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">25000<\/span>;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If however, you declare a variable without initializing it, then the value is set to NULL for static storage. For others, the value is undefined. So it&#8217;s always good to declare your variables.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Why Variables are Declared<\/strong><\/p>\n<p>Declared variables reduces the compilation time. This is because, the compiler knows that there is a variable of the specified type, then linking can proceed.<\/p>\n<p>Also, when multiple files are used, then variable define in one file would be available in another file. This happens during the linking phase.<\/p>\n<p>However for this to work, you need to declare the variable using the extern keyword. This you do in just one place.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Declaration vs Definition Example<\/strong><\/p>\n<p>The example below illustrates the difference between variable declaration and definition.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Variable declaration vs definition<\/span>\r\n<span style=\"color: #888888;\">\/\/By Kindson The Genius<\/span>\r\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #888888;\">\/\/ Variable declarations:<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">extern<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> x, y;\r\n<span style=\"color: #008800; font-weight: bold;\">extern<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> z;\r\n<span style=\"color: #008800; font-weight: bold;\">extern<\/span> <span style=\"color: #333399; font-weight: bold;\">float<\/span> w;\r\n  \r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #888888;\">\/\/ Variable definitions:<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> x, y;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> z;\r\n   <span style=\"color: #333399; font-weight: bold;\">float<\/span> w;\r\n \r\n   <span style=\"color: #888888;\">\/\/ initialization<\/span>\r\n   x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>;\r\n   y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>;\r\n   z <span style=\"color: #333333;\">=<\/span> x <span style=\"color: #333333;\">+<\/span> y;\r\n \r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> z <span style=\"color: #333333;\">&lt;&lt;<\/span> endl ;\r\n\r\n   w <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">70.0<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #6600ee; font-weight: bold;\">3.0<\/span>;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> w <span style=\"color: #333333;\">&lt;&lt;<\/span> endl ;\r\n \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you run the code above, then you will have the output:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000dd; font-weight: bold;\">30<\/span>\r\n<span style=\"color: #6600ee; font-weight: bold;\">23.333<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Lvalues and Rvalues<\/strong><\/h4>\n<p>You probably will hear of these only in C++!<\/p>\n<p>Lvalues and Rvalues are the two types of expressions in C++. So what are they?<\/p>\n<ul>\n<li><strong>lvalue<\/strong> &#8211; these is an expression that refers to a memory location. It can appear in an assignment statement. Either in the left or right hand side.<\/li>\n<li><strong>rvalue<\/strong> &#8211; these is a value that is stored in some memory address. It only can appear on the right hand side of an assignment. Therefore, no value can be assigne to it.<\/li>\n<\/ul>\n<p>For example:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\nage <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">37<\/span>; <span style=\"color: #888888;\">\/\/ age is and lvalue, 37 is an rvalue<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Variables are rvalues and therefore can appear on any side of an assignment statement. However, literals like numbers are lvalues.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We already know that data is stored in memory. So variables allow you to name the storage for your data. Therefore, you as a programmer &hellip; <\/p>\n","protected":false},"author":1,"featured_media":40,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,9],"class_list":["post-39","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-tutorials","tag-lvalue-and-rvalue","tag-variables"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/39\/revisions\/47"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media\/40"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}