{"id":34,"date":"2019-04-12T22:47:27","date_gmt":"2019-04-12T22:47:27","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=34"},"modified":"2019-04-12T22:47:27","modified_gmt":"2019-04-12T22:47:27","slug":"c-data-types","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-types\/","title":{"rendered":"C++ Data Types"},"content":{"rendered":"<p>We would cover C++ Data Types in this lesson under the following topics:<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction to C++ Data Types<\/a><\/li>\n<li><a href=\"#t2\">C++ Built-in Data Types<\/a><\/li>\n<li><a href=\"#t3\">Why do we need Data Types<\/a><\/li>\n<li><a href=\"#t4\">The typedef Keyword<\/a><\/li>\n<li><a href=\"#t5\">The enum Types<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Introduction to C++ Data Types<\/strong><\/h4>\n<p>Data types is quite easy understand but very important. For example, a digit is different from a letter. And so on.<\/p>\n<p>Also, remember that C++ is a strongly typed language. So you need to be aware of the data types available.<\/p>\n<p>Just to remind you, in\u00a0 a strongly typed language, you must specify the data type of your variables.<\/p>\n<p>We discuss more on variables in the next chapter.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. C++ Built-in Data Types<\/strong><\/h4>\n<p>Built-in data types are also called basic data types. C++ provide a number of data types which are listed in the table below:<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th width=\"50%\">Data Type<\/th>\n<th>Keyword<\/th>\n<\/tr>\n<tr>\n<td>Boolean<\/td>\n<td>bool<\/td>\n<\/tr>\n<tr>\n<td>Character<\/td>\n<td>char<\/td>\n<\/tr>\n<tr>\n<td>Integer<\/td>\n<td>int<\/td>\n<\/tr>\n<tr>\n<td>Floating point<\/td>\n<td>float<\/td>\n<\/tr>\n<tr>\n<td>Double floating point<\/td>\n<td>double<\/td>\n<\/tr>\n<tr>\n<td>Valueless<\/td>\n<td>void<\/td>\n<\/tr>\n<tr>\n<td>Wide character<\/td>\n<td>wchar_t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Some of these primitive data types can be modified using any of the modifiers listed below:<\/p>\n<ul>\n<li>short<\/li>\n<li>long<\/li>\n<li>signed<\/li>\n<li>unsigned<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Why do we need data types?<\/strong><\/h4>\n<p>Data types are needed to indicate how much memory is reserved for the data. You can find in the table below, each data type along with the size and range.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Data Type<\/th>\n<th>Width in bytes<\/th>\n<th>Data Range<\/th>\n<\/tr>\n<tr>\n<td>char<\/td>\n<td>1 byte<\/td>\n<td>-127 to 127 or 0 to 255<\/td>\n<\/tr>\n<tr>\n<td>unsigned char<\/td>\n<td>1 byte<\/td>\n<td>0 to 255<\/td>\n<\/tr>\n<tr>\n<td>signed char<\/td>\n<td>1 byte<\/td>\n<td>-127 to 127<\/td>\n<\/tr>\n<tr>\n<td>int<\/td>\n<td>4 bytes<\/td>\n<td>-2147483648 to 2147483647<\/td>\n<\/tr>\n<tr>\n<td>unsigned int<\/td>\n<td>4 bytes<\/td>\n<td>0 to 4294967295<\/td>\n<\/tr>\n<tr>\n<td>signed int<\/td>\n<td>4 bytes<\/td>\n<td>-2147483648 to 2147483647<\/td>\n<\/tr>\n<tr>\n<td>short int<\/td>\n<td>2 bytes<\/td>\n<td>-32768 to 32767<\/td>\n<\/tr>\n<tr>\n<td>unsigned short int<\/td>\n<td><\/td>\n<td>0 to 65,535<\/td>\n<\/tr>\n<tr>\n<td>signed short int<\/td>\n<td><\/td>\n<td>-32768 to 32767<\/td>\n<\/tr>\n<tr>\n<td>long int<\/td>\n<td>4 bytes<\/td>\n<td>-2,147,483,648 to 2,147,483,647<\/td>\n<\/tr>\n<tr>\n<td>signed long int<\/td>\n<td>4 bytes<\/td>\n<td>same as long int<\/td>\n<\/tr>\n<tr>\n<td>unsigned long int<\/td>\n<td>4 bytes<\/td>\n<td>0 to 4,294,967,295<\/td>\n<\/tr>\n<tr>\n<td>float<\/td>\n<td>4 bytes<\/td>\n<td>+\/- 3.4e +\/- 38 (~7 digits)<\/td>\n<\/tr>\n<tr>\n<td>double<\/td>\n<td>8 bytes<\/td>\n<td>+\/- 1.7e +\/- 308 (~15 digits)<\/td>\n<\/tr>\n<tr>\n<td>long double<\/td>\n<td>8 bytes<\/td>\n<td>+\/- 1.7e +\/- 308 (~15 digits)<\/td>\n<\/tr>\n<tr>\n<td>wchar_t<\/td>\n<td>2 or 4 bytes<\/td>\n<td>1 wide character<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>But sometimes, the actual size of the may vary depending on the computer or compiler being used.<\/p>\n<p>You can find out the size of each data type using the sizeof(datatype). The code below prints out the sizes of different variables.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to display size of data types<\/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: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of int: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of char: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">char<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of short int: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">short<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of long int: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">long<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of float: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">float<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of double: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">double<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Size of wchar_t: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #008800; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold;\">wchar_t<\/span>) <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>In this code, the &lt;&lt;endl is used to add a new line after each data type printed.<\/p>\n<p>This code will give an output below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Size of <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">char<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">short<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">long<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">float<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">8<\/span>\r\nSize of <span style=\"color: #333399; font-weight: bold;\">wchar_t<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. The typedef Keyword<\/strong><\/h4>\n<p>Interestingly, you can change the name of the data types. C++ allows you to do that using the typedef keyword.<\/p>\n<p>The syntax is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">typedef<\/span> type newname;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>I don&#8217;t thing you&#8217;ll use this much. So don&#8217;t bother much but just know the syntax.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. enum Types (or Enumerated types)<\/strong><\/h4>\n<p>This is a way to provide a type name for a set of items. For example, you have a list of fruits, say, orange, banana, apple, pear. We want to give them a type called fruit. The we can achieve this using the syntax below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">enum<\/span> fruit {orange, banana, apple, pear};\r\n\r\nfruit f;\r\n\r\nf <span style=\"color: #333333;\">=<\/span> banana;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this case, fruit is an enum type<\/p>\n<p>Now about enum types. The list of items inside the braces have default values starting from 0. Therefore the value of orange is 0, the value of banana is 1 and so on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We would cover C++ Data Types in this lesson under the following topics: Introduction to C++ Data Types C++ Built-in Data Types Why do we &hellip; <\/p>\n","protected":false},"author":1,"featured_media":37,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[8],"class_list":["post-34","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-tutorials","tag-c-data-types"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/34","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=34"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/34\/revisions\/38"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media\/37"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}