{"id":81833,"date":"2021-07-24T09:00:15","date_gmt":"2021-07-24T03:30:15","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81833"},"modified":"2021-07-24T09:00:15","modified_gmt":"2021-07-24T03:30:15","slug":"type-conversion-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/","title":{"rendered":"Type Conversion in C++"},"content":{"rendered":"<p>In this tutorial, we will learn how to type convert in C++.<\/p>\n<h3>Type Conversion in C++<\/h3>\n<p>C++ allows us to convert data from one type to another. This is known as type conversion in C++. Type conversion is mainly done to make variables of one type work with variables of another type in order to carry out an operation.<\/p>\n<p>There are two ways of type conversion:<\/p>\n<p>1. Implicit<br \/>\n2. Explicit<\/p>\n<h4>1. Implicit Type Conversion<\/h4>\n<p>It is also referred to as automatic type conversion as the compiler does it on its own. The user does not need to provide any external trigger. When multiple data types are present in an expression, type conversion occurs to avoid loss of data.<\/p>\n<p>Data types of all variables are upgraded to the largest data type in the expression. Following is the order of implicit type conversion:<\/p>\n<p><strong>bool -&gt; char -&gt; short int -&gt; int -&gt; unsigned int -&gt; long -&gt; unsigned -&gt; long long -&gt; float -&gt; double -&gt; long double<\/strong><\/p>\n<p><strong>Example of implicit type conversion in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int a = 8;\n  char b = 'm';\n  float c = 2.45;\n  float result = a+b+c;   \/\/types of a and b are implicitly converted\n  cout&lt;&lt;result;\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">119.45<\/div>\n<p>In implicit type conversion, data can be lost. For example, when signed is automatically converted to unsigned, we can lose sign or when long long gets converted to float, overflow can take place. This can also happen when a data of larger type is converted to smaller type. For example, when double is converted to int, the decimal part is lost.<\/p>\n<h4>2. Explicit Type Conversion<\/h4>\n<p>This is also known as Type Casting in C++. It is done by the user i.e., we can explicitly convert one type to another.<br \/>\nWe can do this in one of the following ways:<\/p>\n<h4>a. Generic type casting in C++<\/h4>\n<p>We can consider this as forceful casting. There are two syntaxes for generic type casting:<\/p>\n<p><strong>i. Functional notation<\/strong><br \/>\nExpression is kept in parentheses.<br \/>\ntype (expression)<\/p>\n<p><strong>ii. C-like cast notation<\/strong><br \/>\nData type is kept in parentheses.<br \/>\n(type) expression<\/p>\n<p><strong>Example of generic type casting in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  float x = 10.3;\n  int y;\n  cout&lt;&lt;x&lt;&lt;endl;\n  y = int (x);    \/\/functional notation\n  cout&lt;&lt;y&lt;&lt;endl;\n  y = (int) x;    \/\/C-like cast notation\n  cout&lt;&lt;y;\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">10.3<br \/>\n10<br \/>\n10<\/div>\n<h4>b. Using cast operators in C++<\/h4>\n<p>Cast operators are unary operators that convert one type to another. There are four types of cast operators supported in C++:<\/p>\n<p><strong>i. static_cast<\/strong><br \/>\nstatic_cast performs conversion between pointers to classes that are related. It can execute upcast (converting pointer-to-derived to pointer-to-base) as well as downcast (converting pointer-to-base to pointer-to-derived).<\/p>\n<p>During runtime, no checks are made to ensure that the object being converted is a full object of the destination type. As a result, it is the programmer&#8217;s responsibility to ensure safe conversion.<\/p>\n<p><strong>Syntax<\/strong><br \/>\nstatic_cast &lt;new_type&gt; (expression)<\/p>\n<p><strong>ii. dynamic_cast<\/strong><\/p>\n<p>We can use dynamic_cast only with pointers and references to classes (or with void*). Its aim is to check that the type conversion result points to a legitimate full object of the target pointer type. This does upcasting (converting pointer-to-derived to pointer-to-base).<\/p>\n<p>However, it can downcast (converting pointer-to-base to pointer-to-derived) polymorphic classes (classes containing virtual members) only when the pointed object is a legitimate full object of the target type.<\/p>\n<p><strong>Syntax<\/strong><br \/>\ndynamic_cast &lt;new_type&gt; (expression)<\/p>\n<p><strong>iii. const_cast<\/strong><\/p>\n<p>It influences the constancy of the object pointed by a pointer, allowing it to be set or removed.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>const_cast &lt;new_type&gt; (expression)<\/p>\n<p><strong>iv. reinterpret_cast<\/strong><\/p>\n<p>reinterpret_cast changes any pointer type to any other pointer type, regardless of whether or not the classes are related.<\/p>\n<p>A basic binary copy of the value from one pointer to the other is the result of the operation. All pointer conversions are permitted; neither the data pointed to nor the type of pointer is checked.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>reinterpret_cast &lt;new_type&gt; (expression)<\/p>\n<p><strong>Example of type casting using cast operators in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  float x = 24.37;\n  int y = static_cast&lt;int&gt;(x);\n  cout&lt;&lt;y;\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">24<\/div>\n<h3>Advantages of Type Conversion in C++<\/h3>\n<ul>\n<li>Type conversion enables the use of some characteristics of type hierarchies or type representations.<\/li>\n<li>It is useful in computing expressions that include variables of different data types.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>We hope this article helped you understand type conversion in C++. There are two methods of type conversion, implicit and explicit. Implicit type conversion is automatically done by the compiler. We saw how it is done using an example.<\/p>\n<p>Explicit type conversion, also known as Type Casting, is done by the user. We discussed the ways in which we can explicitly type cast with suitable examples. We also learnt that there are four types of cast operators available in C++, static_cast, dynamic_cast, const_cast and reinterpret_cast.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to type convert in C++. Type Conversion in C++ C++ allows us to convert data from one type to another. This is known as type conversion in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82939,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3828,3829,3830],"class_list":["post-81833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-explicit-type-conversion-in-c","tag-implicit-type-conversion-in-c","tag-type-conversion-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Type Conversion in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is type conversion in C++. There are two methods of type conversion, implicit and explicit. Learn more about it with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Type Conversion in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is type conversion in C++. There are two methods of type conversion, implicit and explicit. Learn more about it with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-24T03:30:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Type Conversion in C++ - TechVidvan","description":"Learn what is type conversion in C++. There are two methods of type conversion, implicit and explicit. Learn more about it with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Type Conversion in C++ - TechVidvan","og_description":"Learn what is type conversion in C++. There are two methods of type conversion, implicit and explicit. Learn more about it with examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-24T03:30:15+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Type Conversion in C++","datePublished":"2021-07-24T03:30:15+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/"},"wordCount":745,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg","keywords":["Explicit Type Conversion in C","Implicit Type Conversion in C","Type Conversion in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/","name":"Type Conversion in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg","datePublished":"2021-07-24T03:30:15+00:00","description":"Learn what is type conversion in C++. There are two methods of type conversion, implicit and explicit. Learn more about it with examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Type-Conversion-in-C.jpg","width":1200,"height":628,"caption":"Type Conversion in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/type-conversion-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Type Conversion in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81833"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82939"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}