{"id":23271,"date":"2023-12-17T12:12:26","date_gmt":"2023-12-17T12:12:26","guid":{"rendered":"https:\/\/linuxways.net\/?p=23271"},"modified":"2024-03-25T02:10:27","modified_gmt":"2024-03-25T02:10:27","slug":"type-casting-c-language","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/type-casting-c-language\/","title":{"rendered":"Type Casting in C Language"},"content":{"rendered":"<p>One of the first things that we must learn when dealing with programming in C is that this language deals with different types of data, and that each of them is related to the size it occupies in memory, the bit precision of the system, and the way it is organized in the storage area. The C language is strict in dealing with data types because that is the first thing that is specified when a variable is declared.<\/p>\n<p>When we develop the code in the C language, there are cases when we need to convert the value of a variable from one data type to another like, for example, when we need to use the value that is stored in it as the input or output argument of a function that accepts a different data type.<\/p>\n<p>In some cases, the compiler performs the implicit type conversions that are performed automatically. In other cases, we must perform a casting operation on the variable to convert its type.<\/p>\n<p>In this Linux Ways article, you&#8217;ll learn what data type castings are in variables, arrays, and pointers. We will also explain in which cases the compiler does the casting automatically and when you have to specify it by an explicit operation. In each section, you&#8217;ll find practical examples with code fragments and images that show you everything you need to know about type conversions in the C language.<\/p>\n<h2><a id=\"post-23271-_mijb5j3bd9nb\"><\/a>Implicit Casting in the C Language<\/h2>\n<p>In the C language, arithmetic or assignment operations are always performed between operands of the same data type. If this is not the case, the compiler performs a conversion on the smaller variable to convert its value to the same data type of the other operand.<\/p>\n<p>This is called an implicit cast and it happens automatically when we perform the operations between variables with different data types. In casts, the data type of the variable is not changed, but the compiler converts the size and format of the value to perform the operation. We can see this in the following example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp;a <span class=\"sy0\">=<\/span><span class=\"nu0\">55<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">char<\/span> b<span class=\"sy0\">=<\/span> <span class=\"nu0\">23<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> c<span class=\"sy0\">;<\/span><br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">+<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a + b = %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In this case, an implicit cast of type int was performed in the variable \u201cb\u201d of type char to be able to operate with \u201ca\u201d. Let\u2019s see an image with the result of this operation with implicit casting:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23275\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Let&#8217;s look at the following example in which the variable \u201ca\u201d of type int and the variable \u201cb\u201d of type float perform a division operation of 3 \/ 2 and store their result in the integer \u201cc\u201d:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; a <span class=\"sy0\">=<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> b<span class=\"sy0\">=<\/span> <span class=\"nu0\">23<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> c<span class=\"sy0\">;<\/span><br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">\/<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a \/ b = %f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In this case, the variable of type int is cast to type float. Let\u2019s see an image with the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23278\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the figure, by implicitly casting in the variable \u201ca\u201d, we were able to perform an operation that gave an exact result of division by treating both variables as floating point numbers.<\/p>\n<h2><a id=\"post-23271-_f5aljqg4mx89\"><\/a>Explicit Casting in the C Language<\/h2>\n<p>As we have seen so far, the compiler automatically performs implicit castings when the operands are of different types. However, there are cases where, for example, we need to perform an operation on two variables of the same type, but treat them as variables of a different type. Let&#8217;s look at this with an example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; a <span class=\"sy0\">=<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; b<span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> c<span class=\"sy0\">;<\/span><br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">\/<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a \/ b = %f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In the given code, a division is performed between the variables \u201ca\u201d and \u201cb\u201d of type int, giving the result of 1 and leaving a remainder of 1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23280\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>In these cases, we can treat the variables \u201ca\u201d and \u201cb\u201d as data of type Float and thus obtain an exact result in the variable \u201cc\u201d. To do this, we must explicitly tell the compiler to perform typecasting on them. Let\u2019s see the syntax for explicit casting operations:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"br0\">&#40;<\/span>Type<span class=\"br0\">&#41;<\/span> variable or expresion<\/div><\/div>\n<p>To do the explicit casting, we need to specify the type that we want to cast the variable to in parentheses and then the variable or expression that we want to cast. Let\u2019s see the previous code where we performed a casting to the type float in variables \u201ca\u201d and \u201cb\u201d.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; a <span class=\"sy0\">=<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; b<span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> c<span class=\"sy0\">;<\/span><br \/>\nc <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">float<\/span><span class=\"br0\">&#41;<\/span> a <span class=\"sy0\">\/<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">float<\/span><span class=\"br0\">&#41;<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a \/ b = %f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In this case, the variables \u201ca\u201d and \u201cb\u201d are treated as floating point numbers which leads to an exact result when dividing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23283\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>In this case, the variables \u201ca\u201d and \u201cb\u201d were cast separately. But as we saw earlier in the syntax, we can also cast the expression, which, as we can see in the following, simplifies the code:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; a <span class=\"sy0\">=<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> &nbsp; b<span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> c<span class=\"sy0\">;<\/span><br \/>\nc <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">float<\/span><span class=\"br0\">&#41;<\/span> a <span class=\"sy0\">\/<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a \/ b = %f<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Explicit casting can happen anywhere in the code, be it in an operation, within the arguments that we send to a function, etc.<\/p>\n<h2><a id=\"post-23271-_ftglj259f9ib\"><\/a>Pointers Casting in the C Language<\/h2>\n<p>In C language, pointers also have certain data types. This means that for certain assignment operations, or when we use pointers as input or output arguments to functions, errors may occur at compile time due to type incompatibility. The following code declares a pointer of type char which is then used as the output argument of the getchar() function:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>c<span class=\"sy0\">;<\/span><br \/>\nc<span class=\"sy0\">=<\/span><a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/getchar.html\"><span class=\"kw3\">getchar<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Character: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The getchar() function recovers a character from the input but returns it in the int data type, which is why the compiler issues the following error:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23288\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>In this case, we need to do an explicit casting in the c-pointer. This is done by placing the type of the pointer in front of the \u201c<strong><em>*<\/em><\/strong>\u201d symbol in parentheses as seen in the following code:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>c<span class=\"sy0\">;<\/span><br \/>\nc<span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span><span class=\"br0\">&#41;<\/span> <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/getchar.html\"><span class=\"kw3\">getchar<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Character: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As seen in the following figure, casting the pointer fixes the compilation error that occurred previously:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23291\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-6.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-6-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23271-6-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23271-_wzx5ab5d1s3a\"><\/a>Conclusion<\/h2>\n<p>In this Linux Ways article, we just saw the types of casting that can be done in the C language. We showed you how to distinguish the cases where the compiler performs casting implicitly and automatically, and when you need to specify the type conversion explicitly. Since programming in this language involves a lot of work with pointers, we also included a section on converting this type.<\/p>","protected":false},"excerpt":{"rendered":"<p>Guide on what data type casting is in a variable, array, and pointer and how to specify an explicit operation when the compiler does the casting automatically.<\/p>","protected":false},"author":110,"featured_media":23304,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23271"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23271\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23304"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}