{"id":81602,"date":"2021-07-15T09:00:07","date_gmt":"2021-07-15T03:30:07","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81602"},"modified":"2021-07-15T09:00:07","modified_gmt":"2021-07-15T03:30:07","slug":"constants-and-literals-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/","title":{"rendered":"Constants in C++ | Literals in C++"},"content":{"rendered":"<p>In this article, we will learn about constants and literals in C++.<\/p>\n<h3>What are Constants in C++?<\/h3>\n<p>Constants are expressions whose values remain fixed. Once defined, we cannot change the value of a constant.<\/p>\n<h3>What are Literals in C++?<\/h3>\n<p>The fixed value assigned to a constant is called literal. For example, in constant expression const int x = 10; the value 10 is called constant integer literal.<\/p>\n<p>Following are the types of literals:<\/p>\n<h4>1. Integer Literals in C++<\/h4>\n<p>An integer is a numeric literal that does not contain any fractional or exponential parts. Integer literals are expressed as:<\/p>\n<h5>a. Prefix<\/h5>\n<p>It specifies the base or radix. These are of four types: no prefix for decimal (base 10), 0 for octal (base 8), 0x or 0X for hexadecimal (base 16) and 0b or 0B for binary (base 2).<\/p>\n<p><strong>For example:<\/strong><\/p>\n<ul>\n<li>Decimal: 97, 3, -62, etc.<\/li>\n<li>Octal: 04, 021, 0743, etc.<\/li>\n<li>Hexadecimal: 0x2, 0X4a, 0x56c, etc.<\/li>\n<li>Binary: 0b10, 0B1011, 0b110, etc.<\/li>\n<\/ul>\n<h5>b. Suffix<\/h5>\n<p>It represents the type, u or U for unsigned, l or L for long and combination of both.<\/p>\n<p><strong>For example:<\/strong><\/p>\n<ul>\n<li>42 \/\/int requires no suffix<\/li>\n<li>42u \/\/unsigned int<\/li>\n<li>42ul \/\/unsigned long<\/li>\n<li>42L \/\/long<\/li>\n<li>42ll \/\/long long<\/li>\n<\/ul>\n<h4>2. Floating-point Literals in C++<\/h4>\n<p>Floating-point literals, which represent real numbers, have an integer part, a real component, a fractional component, and an exponential part. We can store it either in decimal or in exponential form.<\/p>\n<p>We must remember that<\/p>\n<ul>\n<li>The decimal point, exponential part, or both must be included in the decimal form; otherwise, an error will occur.<\/li>\n<li>In the exponential form, the integer, fractional, or both parts must be included; otherwise, an error will occur.<\/li>\n<\/ul>\n<p>Some examples of valid floating-point literals are 12.365, 2.67E-6, etc.<\/p>\n<p>Some examples of invalid floating-point literals are 256E, 0.e34, etc.<\/p>\n<h4>3. Character Literals in C++<\/h4>\n<p>Character literals store a single character enclosed within a single quote. These have two representations:<\/p>\n<ul>\n<li>Normal or narrow character literal of char type. For example, &#8216;i&#8217;.<\/li>\n<li>Wide character literal of wchar_t type which begins with uppercase L. For example, L&#8217;i&#8217;.<\/li>\n<\/ul>\n<p>There are various characters in C++ that have special meaning. These character literals are Escape Sequences. Following is a list of some escape sequences in C++.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Escape Sequence<\/b><\/td>\n<td><b>Meaning<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\a<\/span><\/td>\n<td><span style=\"font-weight: 400\">Alert or beep<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\b<\/span><\/td>\n<td><span style=\"font-weight: 400\">Backspace<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\f<\/span><\/td>\n<td><span style=\"font-weight: 400\">Form feed<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\n<\/span><\/td>\n<td><span style=\"font-weight: 400\">Newline<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\r<\/span><\/td>\n<td><span style=\"font-weight: 400\">Carriage return<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\t<\/span><\/td>\n<td><span style=\"font-weight: 400\">Horizontal tab<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\v<\/span><\/td>\n<td><span style=\"font-weight: 400\">Vertical tab<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\\\<\/span><\/td>\n<td><span style=\"font-weight: 400\">Backlash<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\&#8217;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single quote<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\&#8221;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Double quote<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\?<\/span><\/td>\n<td><span style=\"font-weight: 400\">Question mark<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Null character<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\ooo<\/span><\/td>\n<td><span style=\"font-weight: 400\">Octal number\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\\xhh<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hexadecimal number\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example to illustrate the use of escape sequences in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  cout&lt;&lt;\"TechVidvan\\nGoogle\\n\";\n  cout&lt;&lt;\"Tech\\tVidvan\";\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">TechVidvan<br \/>\nGoogle<\/div>\n<div class=\"code-output\">Tech Vidvan<\/div>\n<h4>4. String Literals in C++<\/h4>\n<p>String literals store multiple characters enclosed in double quotes. They can also include special characters, escape sequences and whitespaces.<\/p>\n<p><strong>For example<\/strong>: &#8220;TechVidvan&#8221;, &#8220;Data Flair&#8221;, &#8220;Hello World!\\n&#8221;, etc.<\/p>\n<h4>5. Boolean Literals in C++<\/h4>\n<p>These represent boolean values, which are of two types:<\/p>\n<p><strong>true:<\/strong> represents true value. We should not consider its value equal to int 1.<\/p>\n<p><strong>false:<\/strong> represents false value. We should not consider its value equal to int 0.<\/p>\n<h3>Defining Constants in C++<\/h3>\n<p>We can define a constant in one of the two ways,<\/p>\n<h4>1. Using const keyword<\/h4>\n<p>We can define a constant using const keyword as prefix.<br \/>\n<strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const datatype name = value;<\/pre>\n<p><strong>Example to define constants using const<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  const int a = 10;       \/\/integer literal = 10\n  const float b = 50.74;      \/\/floating point literal = 50.74\n  const char newline = '\\n';      \/\/character literal = \\n\n  \n  cout&lt;&lt;\"Defined constants:\"&lt;&lt;newline;\n  cout&lt;&lt;a&lt;&lt;newline;\n  cout&lt;&lt;b;\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Defined constants:<br \/>\n10<br \/>\n50.74<\/div>\n<p>Let\u2019s try to modify value of a constant<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  const int a = 10;\n  a = 20;\n  cout&lt;&lt;a;\n  return 0;\n}\n<\/pre>\n<p><strong>Status<\/strong> Compilation error<br \/>\n<strong>Output<\/strong><\/p>\n<div class=\"code-output\">In function \u2018int main()\u2019:<br \/>\nerror: assignment of read-only variable \u2018a\u2019<\/div>\n<p>We get a compilation error that variable a is read-only.<\/p>\n<h4>2. Using #define preprocessor directive<\/h4>\n<p>We can use #define preprocessor directive to declare constants.<br \/>\n<strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define name value<\/pre>\n<p><strong>Example to define constants using #define<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\n#define length 12.5\n#define breadth 8\n#define unit \"m sq.\"\n\nint main() {\n  double area;\n  area = length * breadth;\n  cout&lt;&lt;area&lt;&lt;\" \"&lt;&lt;unit;\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">100 m sq.<\/div>\n<h3>Summary<\/h3>\n<p>In this article, we learnt what constants are. We talked about literals in C++. Literals can be of integer, floating-point, character, string or boolean type. There are two ways in which we can define constants. Using suitable examples, we learnt both these ways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about constants and literals in C++. What are Constants in C++? Constants are expressions whose values remain fixed. Once defined, we cannot change the value of a constant.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81819,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3766,3767,3768,3769],"class_list":["post-81602","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-boolean-literals-in-c","tag-character-literals-in-c","tag-constants-in-c","tag-literals-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constants in C++ | Literals in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about constants and literals using suitable examples in C++. Learn about the types of literals and ways to define constants in C++.\" \/>\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\/constants-and-literals-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constants in C++ | Literals in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about constants and literals using suitable examples in C++. Learn about the types of literals and ways to define constants in C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-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-15T03:30:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-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":"Constants in C++ | Literals in C++ - TechVidvan","description":"Learn about constants and literals using suitable examples in C++. Learn about the types of literals and ways to define constants in C++.","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\/constants-and-literals-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Constants in C++ | Literals in C++ - TechVidvan","og_description":"Learn about constants and literals using suitable examples in C++. Learn about the types of literals and ways to define constants in C++.","og_url":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-15T03:30:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-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\/constants-and-literals-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Constants in C++ | Literals in C++","datePublished":"2021-07-15T03:30:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/"},"wordCount":619,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C.jpg","keywords":["Boolean Literals in C++","Character Literals in C++","Constants in C++","Literals in C"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/","name":"Constants in C++ | Literals in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C.jpg","datePublished":"2021-07-15T03:30:07+00:00","description":"Learn about constants and literals using suitable examples in C++. Learn about the types of literals and ways to define constants in C++.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Constants-and-Literals-in-C.jpg","width":1200,"height":628,"caption":"Constants and Literals in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/constants-and-literals-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Constants in C++ | Literals 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\/81602","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=81602"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81602\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81819"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}