{"id":2321,"date":"2017-04-19T20:04:50","date_gmt":"2017-04-19T19:04:50","guid":{"rendered":"http:\/\/x-engineer.org\/?p=2321"},"modified":"2021-11-11T00:24:38","modified_gmt":"2021-11-10T23:24:38","slug":"data-types-c","status":"publish","type":"post","link":"https:\/\/x-engineer.org\/data-types-c\/","title":{"rendered":"What are the data types in C"},"content":{"rendered":"<p>One of the most important concept in programming is the <strong>variable<\/strong>. The variable can be seen as the &#8220;place&#8221; to store &#8220;things&#8221; as: numerical values, characters, text strings, memory addresses, etc. There are two main concepts regarding variables.\u00a0The fist concept is the <strong>declaration<\/strong> of the variable, which basically means setting its <strong>data type<\/strong>. The second concept is the <strong>definition<\/strong>\u00a0of the variable, which means setting its <strong>content<\/strong>.<\/p>\n<p>In the\u00a0<a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/introduction-to-c-programming\/\" target=\"_blank\" rel=\"noopener noreferrer\">C programming language<\/a> every\u00a0variable used in the source code needs to be declared by setting its <strong>data type<\/strong>. By assigning a certain data type to a variable we define\u00a0several aspects linked to the variable:<\/p>\n<ul>\n<li>the memory size to be allocated to\u00a0store the content of the variable<\/li>\n<li>the types of operations that can be performed on the variable<\/li>\n<li>the restrictions which are applied in terms of operations<\/li>\n<\/ul>\n<p>By the end of this tutorial the reader will know:<\/p>\n<ul>\n<li>what is the significance of a data type<\/li>\n<li>how to declare and define a variable<\/li>\n<li>which are the properties of the standard data types<\/li>\n<li>what is integer overflow<\/li>\n<\/ul>\n<p>In the <a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/how-c-programming-works-from-editor-to-executable\/\" target=\"_blank\" rel=\"noopener noreferrer\">C programming language<\/a> a variable is <strong>declared<\/strong> as:<\/p>\n<pre>unsigned int uiVar;<\/pre>\n<p>where:<\/p>\n<p><code>uiVar<\/code> &#8211; is the name of the variable<br \/>\n<code>unsigned<\/code> &#8211; keyword which defines that our variable is always positive (without the sign &#8220;-&#8220;)<br \/>\n<code>int<\/code> &#8211; keyword which defines that our variable is an integer and has 4 bytes of memory allocated (for a 32-bit compiler)<\/p>\n<p>The <strong>definition<\/strong> of the variable can be done in another line, but only after the declaration:<\/p>\n<pre>uiVar = 25;<\/pre>\n<p>Variables can be <strong>declared and defined<\/strong> in the same instruction:<\/p>\n<pre>unsigned int uiVar = 25;<\/pre>\n<p>The main data types in <a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/the-anatomy-of-a-c-code-source-file\/\" target=\"_blank\" rel=\"noopener noreferrer\">C programming language<\/a> are:<\/p>\n<ul>\n<li><strong>integer (fixed-point)<\/strong><\/li>\n<li><strong>floating-point<\/strong><\/li>\n<li><strong>void<\/strong><\/li>\n<\/ul>\n<div id=\"attachment_2318\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-standard-data-types.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2318\" class=\"wp-image-2318\" title=\"C programming language - standard data types\" src=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-standard-data-types.jpg\" alt=\"C programming language - standard data types\" width=\"630\" height=\"223\" srcset=\"https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-standard-data-types.jpg 1223w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-standard-data-types-300x106.jpg 300w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-standard-data-types-1024x362.jpg 1024w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><p id=\"caption-attachment-2318\" class=\"wp-caption-text\">Image: C programming language &#8211; standard data types<\/p><\/div>\n<p><strong>Note<\/strong>: Depending on the type of the compiler (16-bit or 32-bit), the size and range of <code>int<\/code>, <code>short<\/code> and <code>long<\/code> are different.<\/p>\n<p>The <strong>void<\/strong> type has no value and unknown size. It&#8217;s mainly used for function definition as return type or argument of the function.<\/p>\n<h3>Fixed-point (integers)<\/h3>\n<p>The <strong>integer<\/strong> data type is also knows as <strong>fixed-point<\/strong> type. Variables declared as integers store only integer numbers (e.g. <code>-10<\/code>, <code>0<\/code>, <code>55<\/code>), they can not store real values (e.g <code>1.5<\/code>, <code>3.1416<\/code>).<\/p>\n<p>In the example below we&#8217;ll declare a variable of type integer and we&#8217;ll assign a non integer value to it. With an <code>if<\/code> statement we check if the value memorised in the variable is only the integer part of the value, if true we print a message.<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n\tunsigned int uiVar;\r\n\r\n\tuiVar = 3.1416;\r\n\r\n\tif (uiVar == 3)\r\n\t{\r\n\t\tprintf(\"Integer variables can not store decimals\\n\");\r\n\t}\r\n\r\n\treturn (0);\r\n}\r\n<\/pre>\n<p>As expected, the comparison <code>uiVar==3<\/code> is true (since the message is displayed). This means that only the integer part of the number <code>3.1416<\/code> was memorised, since <code>uiVar<\/code>\u00a0was declared as\u00a0an integer.<\/p>\n<p>An integer can be <code>signed<\/code> or <code>unsigned<\/code>. This mean that it can contain both <strong>positive and negative<\/strong> numbers (<code>signed<\/code>) or only <strong>positive<\/strong> numbers (<code>unsigned<\/code>). Function of the number of bytes used to store the content, integer variables can be:<\/p>\n<ul>\n<li><code>char<\/code><\/li>\n<li><code>short<\/code><\/li>\n<li><code>int<\/code><\/li>\n<li><code>long<\/code><\/li>\n<li><code>long long<\/code><\/li>\n<\/ul>\n<p>For a 32-bit compiler, the\u00a0differences between the integer data types are summarised in the table below:<\/p>\n<table>\n<tbody style=\"font-size: smaller; vertical-align: middle;\">\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Data type<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong># bytes<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong># bits<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\" colspan=\"2\"><strong>Minimum value<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\" colspan=\"2\"><strong>Maximum value<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Format<\/strong><br \/>\n<code>printf()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>char<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>7<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-128<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>7<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">127<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>\u00a0<code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>signed char<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>7<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-128<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>7<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">127<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>\u00a0<code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>unsigned char<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>8<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">255<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%u<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>short<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">16<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>15<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-32768<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>15<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32767<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>signed short<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">16<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>15<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-32768<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>15<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32767<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>unsigned short<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">16<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>16<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">65535<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%u<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>int<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>31<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2147483648<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>31<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2147483647<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>signed int<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>31<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2147483648<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>31<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2147483647<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%d<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>unsigned int<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>32<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4294967295<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%u<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>31<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2147483648<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>31<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2147483647<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%ld<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>signed long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>31<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2147483648<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>31<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2147483647<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%ld<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>unsigned long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">32<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>32<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4294967295<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%lu<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>long long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">64<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>63<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-9223372036854775808<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>63<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">9223372036854775807<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%lld<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>signed long long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">64<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-2<sup>63<\/sup><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">-9223372036854775808<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>63<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">9223372036854775807<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%lld<\/code><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>unsigned long long<\/code><\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">64<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">0<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2<sup>64<\/sup>-1<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">18446744073709551615<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong><code>%llu<\/code><\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Note<\/strong>: The number of bytes applies to both X86 (32-bit) and X64 (64-bit) compilers.<\/p>\n<p>The usage of the <code>signed<\/code> keyword is <strong>optional<\/strong>. If we don&#8217;t specify the sign, the default setting is <code>signed<\/code>. For example <code>int<\/code> is the same as <code>signed int<\/code> and <code>long<\/code> the same as <code>signed long<\/code>.<\/p>\n<div id=\"attachment_2319\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-signed-integer-data-types.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2319\" class=\"wp-image-2319\" title=\"C programming language - signed integer data types\" src=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-signed-integer-data-types.jpg\" alt=\"C programming language - signed integer data types\" width=\"630\" height=\"129\" srcset=\"https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-signed-integer-data-types.jpg 1471w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-signed-integer-data-types-300x61.jpg 300w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-signed-integer-data-types-1024x210.jpg 1024w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><p id=\"caption-attachment-2319\" class=\"wp-caption-text\">Image: C programming language &#8211; signed integer data types<\/p><\/div>\n<p>Variables of type <code>signed<\/code> use half of the range for negative values and the other half for positive values. When <a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/the-anatomy-of-a-c-code-source-file\/\" target=\"_blank\" rel=\"noopener noreferrer\">coding in C<\/a> we need to pay attention if we really need to use <code>signed<\/code> data types for our variables. If, for example, we define a variable to measure\/calculate a\u00a0fluid&#8217;s pressure, there is no point of using signed data type because the pressure is always positive. By using unsigned data types we double the range of the variable&#8217;s values.<\/p>\n<div id=\"attachment_2320\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-unsigned-integer-data-types.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2320\" class=\"wp-image-2320\" title=\"C programming language - unsigned integer data types\" src=\"http:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-unsigned-integer-data-types.jpg\" alt=\"C programming language - unsigned integer data types\" width=\"630\" height=\"128\" srcset=\"https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-unsigned-integer-data-types.jpg 1469w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-unsigned-integer-data-types-300x61.jpg 300w, https:\/\/x-engineer.org\/wp-content\/uploads\/2017\/04\/C-programming-language-unsigned-integer-data-types-1024x208.jpg 1024w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><p id=\"caption-attachment-2320\" class=\"wp-caption-text\">Image: C programming language &#8211; unsigned integer data types<\/p><\/div>\n<h3>Floating-point<\/h3>\n<p>Floating-point values are stored in the single-precision or double-precision format specified by the <strong>IEEE Standard 754<\/strong>. Real numbers contain also decimal point numbers so they need to be stored in floating-point variables.<\/p>\n<p>There are three types of floating-point data types:<\/p>\n<ul>\n<li><code>float<\/code><\/li>\n<li><code>double<\/code><\/li>\n<li><code>long double<\/code><\/li>\n<\/ul>\n<p><code>float<\/code> data type is also knows as <strong>single-precision<\/strong> data type. <code>double<\/code> is a enhanced version of <code>float<\/code>, with bigger memory size, wider range and <strong>double-precision<\/strong>.<\/p>\n<table>\n<tbody style=\"font-size: smaller;\">\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Data types<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong># bytes<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Minimum value<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Maximum value<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Precision<\/strong><\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><strong>Format<\/strong><br \/>\n<code>printf()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><code>float<\/code><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">4<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1.2E-38<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">3.4E+38<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">6 decimal places<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><code>%f<\/code>, <code>%e<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><code>double<\/code><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">8<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">2.3E-308<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1.7E+308<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">15 decimal places<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><code>%lf<\/code>, <code>%le<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center; vertical-align: middle;\"><code>long double<\/code><\/td>\n<td style=\"text-align: center; vertical-align: middle;\">10<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">3.4E-4932<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">1.1E+4932<\/td>\n<td style=\"text-align: center; vertical-align: middle;\">19 decimal places<\/td>\n<td style=\"text-align: center; vertical-align: middle;\"><code>%Lf<\/code>, <code>%Le<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Data type limits<\/h3>\n<p>There are two header files (<code>*.h<\/code>) which allows the user to gain precise information regarding the standard data types. To include the content of these header files, we need to write the\u00a0following instructions at the beginning of our <a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/the-anatomy-of-a-c-code-source-file\/\" target=\"_blank\" rel=\"noopener noreferrer\">C source code<\/a>\u00a0file (<code>*.c<\/code>):<\/p>\n<pre>#include &lt;limits.h&gt;\r\n#include &lt;float.h&gt;<\/pre>\n<p>In the macros defined in\u00a0<code>limits.h<\/code> header file we can find the limits (range) of the standard data types used in <a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/how-c-programming-works-from-editor-to-executable\/\" target=\"_blank\" rel=\"noopener noreferrer\">C programming language<\/a>. The macros are constants with predefined names (e.g <code>INT_MIN<\/code>), which contain the numerical values of the data types limits. We can use these macros to check if our defined variables exceed the maximum allowable value for a particular data type.\u00a0For example an <code>unsigned char<\/code> variable can not store a value bigger than <code>UCHAR_MAX<\/code> (<code>255<\/code>).<\/p>\n<h3>Example of different data types variables<\/h3>\n<p>In the source code below we can find different types of variables (fixed-point and floating-point), declared and defined in the same instruction. These are defined with the maximum and minimum value of each data type.<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;limits.h&gt;\r\n#include &lt;float.h&gt;\r\n\r\nint main(void)\r\n{\r\n\t\t\tchar \t\tcVar\t= SCHAR_MIN;\r\n\tunsigned\tchar\t\tucVar\t= UCHAR_MAX;\r\n\tsigned \t\tchar \t\tscVar \t= SCHAR_MAX;\r\n\t\t\tshort\t\tsVar\t= SHRT_MIN;\r\n\tunsigned\tshort\t\tusVar\t= USHRT_MAX;\r\n\tsigned\t\tshort\t\tssVar\t= SHRT_MAX;\r\n\t\t\tint\t\tiVar\t= INT_MIN;\r\n\tunsigned\tint\t\tuiVar\t= UINT_MAX;\r\n\tsigned\t\tint\t\tsiVar\t= INT_MAX;\r\n\t\t\tlong\t\tlVar\t= LONG_MIN;\r\n\tunsigned\tlong\t\tulVar\t= ULONG_MAX;\r\n\tsigned\t\tlong\t\tslVar\t= LONG_MAX;\r\n\t\t\tlong long\tllVar\t= LLONG_MIN;\r\n\tunsigned\tlong long\tullVar\t= ULLONG_MAX;\r\n\tsigned\t\tlong long\tsllVar\t= LLONG_MAX;\r\n\t\t\tfloat\t\tfVar\t= FLT_MAX;\r\n\t\t\tdouble\t\tdVar\t= DBL_MAX;\r\n\t\t\tlong double\tldVar\t= LDBL_MAX;\r\n\t\r\n\tprintf(\"SCHAR_MIN \t\\t %d\\n\",\tcVar);\r\n\tprintf(\"UCHAR_MAX \t\\t %u\\n\",\tucVar);\r\n\tprintf(\"SCHAR_MAX \t\\t %d\\n\",\tscVar);\r\n\tprintf(\"SHRT_MIN \t\\t %d\\n\",\tsVar);\r\n\tprintf(\"USHRT_MAX \t\\t %u\\n\",\tusVar);\r\n\tprintf(\"SHRT_MAX \t\\t %d\\n\",\tssVar);\r\n\tprintf(\"INT_MIN \t\\t %d\\n\",\tiVar);\r\n\tprintf(\"UINT_MAX \t\\t %u\\n\",\tuiVar);\r\n\tprintf(\"INT_MAX \t\\t %d\\n\",\tsiVar);\r\n\tprintf(\"LONG_MIN \t\\t %ld\\n\",\tlVar);\r\n\tprintf(\"ULONG_MAX \t\\t %lu\\n\",\tulVar);\r\n\tprintf(\"LONG_MAX \t\\t %ld\\n\",\tslVar);\r\n\tprintf(\"LLONG_MIN \t\\t %lld\\n\",\tllVar);\r\n\tprintf(\"ULLONG_MAX\t\\t %llu\\n\",\tullVar);\r\n\tprintf(\"LLONG_MAX \t\\t %lld\\n\",\tsllVar);\r\n\tprintf(\"FLT_MAX \t\\t %e\\n\",\tfVar);\r\n\tprintf(\"DBL_MAX \t\\t %le\\n\",\tdVar);\r\n\tprintf(\"LDBL_MAX \t\\t %Le\\n\",\tldVar);\r\n\r\n\treturn (0);\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/x-engineer.org\/graduate-engineering\/programming-languages\/c\/how-c-programming-works-from-editor-to-executable\/\" target=\"_blank\" rel=\"noopener noreferrer\">Compiling<\/a> and running the executable displays the following text:<br \/>\n<code><br \/>\nSCHAR_MIN -128<br \/>\nUCHAR_MAX 255<br \/>\nSCHAR_MAX 127<br \/>\nSHRT_MIN -32768<br \/>\nUSHRT_MAX 65535<br \/>\nSHRT_MAX 32767<br \/>\nINT_MIN -2147483648<br \/>\nUINT_MAX 4294967295<br \/>\nINT_MAX 2147483647<br \/>\nLONG_MIN -2147483648<br \/>\nULONG_MAX 4294967295<br \/>\nLONG_MAX 2147483647<br \/>\nLLONG_MIN -9223372036854775808<br \/>\nULLONG_MAX 18446744073709551615<br \/>\nLLONG_MAX 9223372036854775807<br \/>\nFLT_MAX 3.402823e+38<br \/>\nDBL_MAX 1.797693e+308<br \/>\nLDBL_MAX 1.797693e+308<\/code><\/p>\n<p>One important observation is that, for the compiler used in this example, the <code>double<\/code> and <code>long double<\/code> data type have the same maximum value even if they are defined by different macros, <code>DBL_MAX<\/code> and <code>LDBL_MAX<\/code> respectively.<\/p>\n<h3>Integer\u00a0overflow<\/h3>\n<p>What is happening if, by mistake, we exceed the limits contained in a certain data type. For example, we declare a variable as <code>unsigned char<\/code> and assign the value of <code>255<\/code>. If we add <code>1<\/code> to this variable the result will be <code>256<\/code> which will be outside the range of <code>unsigned char<\/code>.<\/p>\n<p><strong>Integer overflow<\/strong> occurs when the value of an integer type variable exceeds its maximum limits.\u00a0To understand what&#8217;s happening let&#8217;s look at the following example:<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;limits.h&gt; \r\n\r\nint main(void)\r\n{\r\n\r\n\tunsigned char ucVar1, ucVar2;\r\n\tsigned char cVar3, cVar4;\r\n\r\n\tucVar1 = UCHAR_MAX;\r\n\tucVar2 = ucVar1 + 1;\r\n\r\n\tcVar3 = SCHAR_MIN;\r\n\tcVar4 = cVar3 - 1;\r\n\r\n\tprintf(\"ucVar1 = %u\\n\", ucVar1);\r\n\tprintf(\"ucVar2 = %u\\n\", ucVar2);\r\n\tprintf(\"cVar3 = %d\\n\", cVar3);\r\n\tprintf(\"cVar4 = %d\\n\", cVar4);\r\n\r\n\treturn (0);\r\n\r\n}\r\n<\/pre>\n<p>In this example we declared four variables, two <code>unsigned char<\/code> and two <code>signed char<\/code>. The usage of the <code>signed<\/code> keyword is optional, the same behaviour is obtained without it. The idea behind the example is to assign the maximum value of the data type to one variable and then add\/subtract one unit for the second variable of the same data type.<\/p>\n<p>Running the executable outputs the following results:<\/p>\n<p><code>ucVar1 = 255<\/code><br \/>\n<code>ucVar2 = 0<\/code><br \/>\n<code>cVar3 = -128<\/code><br \/>\n<code>cVar4 = 127<\/code><\/p>\n<p>If we perform an\u00a0arithmetic operation which produces a result larger than the maximum above for a <strong>N-bit integer<\/strong>, an integer overflow occurs, which reduces the result to modulo <strong>N-th power<\/strong> of <code>2<\/code>, keeping only the least significant bits of the result and effectively causing a wrap around (reset).\u00a0This behaviour\u00a0may cause security problems leading to unexpected results\u00a0and arbitrary code execution.<\/p>\n<p>For any questions or observations regarding this tutorial please use the comment form below.<\/p>\n<p>Don\u2019t forget to Like, Share and Subscribe!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most important concept in programming is the variable. The variable can be seen as the &#8220;place&#8221; to store &#8220;things&#8221; as: numerical values, characters, text strings, memory addresses, etc. There are two main concepts regarding variables.\u00a0The fist concept is the declaration of the variable, which basically means setting its data type. The second concept is the definition\u00a0of the variable, which means setting its content. In the\u00a0C programming language every\u00a0variable used in the source code needs to be declared by setting its data type. By assigning a certain data type to a variable we define\u00a0several aspects linked to the <\/p>\n","protected":false},"author":2,"featured_media":2318,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[620,311,300,617,619,618],"class_list":["post-2321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","tag-c-coding","tag-c-language","tag-c-programming","tag-data-types","tag-float","tag-integer","has_thumb"],"_links":{"self":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/2321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/comments?post=2321"}],"version-history":[{"count":5,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/2321\/revisions"}],"predecessor-version":[{"id":4501,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/2321\/revisions\/4501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/media\/2318"}],"wp:attachment":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/media?parent=2321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/categories?post=2321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/tags?post=2321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}