{"id":1972,"date":"2024-05-10T06:55:53","date_gmt":"2024-05-10T06:55:53","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=1972"},"modified":"2024-05-10T06:55:53","modified_gmt":"2024-05-10T06:55:53","slug":"literals-in-c-c-with-examples","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/literals-in-c-c-with-examples\/","title":{"rendered":"Literals In C\/C++ With Examples"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#literals-in-c-c-with-examples\">Literals In C\/C++ With Examples<\/a><\/li><li ><a href=\"#literals-in-c-c\">Literals In C\/C++<\/a><\/li><li ><a href=\"#1-integer-literals\">1.Integer Literals <\/a><\/li><li ><a href=\"#2-floating-point-literals\">2) Floating-Point Literals<\/a><\/li><li ><a href=\"#3-character-literal\">3) Character Literal<\/a><\/li><li ><a href=\"#4-string-literals\">4) String Literals<\/a><\/li><li ><a href=\"#5-boolean-literals\">5) Boolean Literals<\/a><\/li><li ><a href=\"#faq-literals-in-c-c-with-examples\">FAQ- Literals In C\/C++ With Examples<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"literals-in-c-c-with-examples\">Literals In C\/C++ With Examples<\/h2>\n\n\n\n<p>Literals in the C and C++ programming languages are fundamental building blocks that represent constant values directly in code. These constants can include integers, floating-point numbers, characters, and more. Literals are essential for initializing variables, specifying values in expressions, and conveying data directly within the source code. They provide a means to express concrete values without requiring variables or complex computations. In this exploration of literals in C\/C++, we will delve into the various types of literals and provide illustrative examples to demonstrate their usage and significance in these programming languages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"literals-in-c-c\">Literals In C\/C++<\/h2>\n\n\n\n<p>Literals in programming, whether in C or C++, are indeed constant values assigned to variables that remain fixed and unmodifiable throughout the program&#8217;s execution. Unlike variables, literals are not associated with references and directly hold their values in memory. In common usage, the terms &#8220;constants&#8221; and &#8220;literals&#8221; are often used interchangeably.<\/p>\n\n\n\n<p>For instance, consider the expression &#8220;const int x = 5;,&#8221; where the value 5 is a constant integer literal. In the world of C, there are four primary types of literals, while in C++, there are five:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Integer Literal:<\/strong> Used to represent whole numbers, such as <code>42<\/code>.<\/li>\n\n\n\n<li><strong>Float Literal:<\/strong> Represents decimal numbers with a fractional part, like <code>3.14<\/code>.<\/li>\n\n\n\n<li><strong>Character Literal:<\/strong> Represents a single character enclosed in single quotes, such as <code>'A'<\/code>.<\/li>\n\n\n\n<li><strong>String Literal:<\/strong> Represents a sequence of characters enclosed in double quotes, like <code>\"Hello, World!\"<\/code>.<\/li>\n\n\n\n<li><strong>Boolean Literal (C++):<\/strong> Represents either <code>true<\/code> or <code>false<\/code>, indicating logical values in C++.<\/li>\n<\/ol>\n\n\n\n<p>These literals play a pivotal role in coding by providing a concise way to embed fixed values directly into the source code, enhancing readability and maintainability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-integer-literals\">1.Integer Literals <\/h2>\n\n\n\n<p>Integer literals serve as a means to represent and store integer values in programming. They can be expressed in two primary types:<\/p>\n\n\n\n<p>A) <strong>Prefixes:<\/strong> The use of prefixes with integer literals indicates the base or radix in which the literal is to be interpreted or read. These prefixes provide information about the numeric base of the integer value, which can be essential when working with numbers in different bases.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0x10 = 16\n\nBecause 0x prefix represents a HexaDecimal base. So 10 in HexaDecimal is 16 in Decimal. Hence the value 16.<\/code><\/pre>\n\n\n\n<p><strong>Integer Literals are of 2 types:<\/strong><\/p>\n\n\n\n<p>a. <strong>Decimal-literal(base 10)<\/strong><\/p>\n\n\n\n<p>It consists of a non-zero decimal digit followed by zero or more decimal digits, where decimal digits include the numbers 0 through 9. This is the common representation for integers in these programming languages, allowing you to express a wide range of positive integer values.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>56, 78<\/code><\/pre>\n\n\n\n<p><strong>&nbsp;b. Octal-literal(base 8):<\/strong><\/p>\n\n\n\n<p>It begins with a &#8216;0&#8217; (zero) followed by zero or more octal digits, where octal digits include the numbers 0 through 7. This representation allows you to specify integer values in octal base, which is base 8. Octal literals are less commonly used than decimal literals but can be useful in certain situations, especially when dealing with permissions and bit manipulation.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>045, 076, 06210<\/code><\/pre>\n\n\n\n<p><strong>d. Binary-literal(base 2)<\/strong><\/p>\n\n\n\n<p>It starts with either &#8216;0b&#8217; or &#8216;0B&#8217; followed by one or more binary digits, which are 0 and 1. This notation is used to represent integer values in binary base, which is base 2. Binary literals are valuable for expressing binary-encoded data or when working with low-level bit manipulation operations.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0b101, 0B111<\/code><\/pre>\n\n\n\n<p>B) Suffix<\/p>\n\n\n\n<p>Suffixes are added to the literal to specify its data type<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12345678901234LL \n\nindicates a long long integer value 12345678901234 because of the suffix LL<\/code><\/pre>\n\n\n\n<p>These are represented in many ways according to their data types.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>int:<\/strong> No suffix is required, as an integer constant is assigned as an <code>int<\/code> by default.<\/li>\n\n\n\n<li><strong>unsigned int:<\/strong> Append &#8216;u&#8217; or &#8216;U&#8217; at the end of an integer constant.<\/li>\n\n\n\n<li><strong>long int:<\/strong> Add &#8216;l&#8217; or &#8216;L&#8217; at the end of an integer constant.<\/li>\n\n\n\n<li><strong>unsigned long int:<\/strong> Include &#8216;ul&#8217; or &#8216;UL&#8217; at the end of an integer constant.<\/li>\n\n\n\n<li><strong>long long int:<\/strong> Use &#8216;ll&#8217; or &#8216;LL&#8217; as a suffix for an integer constant.<\/li>\n\n\n\n<li><strong>unsigned long long int:<\/strong> Employ &#8216;ull&#8217; or &#8216;ULL&#8217; as a suffix for an integer constant.<\/li>\n<\/ul>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n  \nint main()\n{\n  \n    \/\/ constant integer literal\n    const int intVal = 10;\n  \n    printf(\"Integer Literal:%d \\n\", intVal);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer Literal:10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-floating-point-literals\">2) Floating-Point Literals<\/h2>\n\n\n\n<p>Floating-point literals are used to represent and store real numbers, which consist of an integer part, real part, fractional part, and exponential part. These literals can be represented in either decimal form or exponential form.<\/p>\n\n\n\n<p>When creating floating-point literals, it&#8217;s essential to adhere to certain rules:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Decimal Form:<\/strong> If representing a floating-point number in decimal form, it should include the decimal point, exponent part, or both. Failing to include these components may result in an error.<\/li>\n\n\n\n<li><strong>Exponential Form:<\/strong> When using exponential form, ensure that it includes the integer part, fractional part, or both, as required. Omitting any of these parts can lead to an error.<\/li>\n<\/ol>\n\n\n\n<p>Adhering to these guidelines helps maintain the accuracy and validity of floating-point literals, preventing potential errors in the code.<\/p>\n\n\n\n<p> Floating-point literal representations are shown below:<\/p>\n\n\n\n<p><strong>Valid Floating Literals:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10.125\n1.215-10L\n10.5E-3<\/code><\/pre>\n\n\n\n<p><strong>Invalid Floating Literals:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>123E\n1250f\n0.e879<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n  \nint main()\n{\n    \/\/ Real literal\n    const float floatVal = 4.14;\n  \n    cout &lt;&lt; \"Floating-point literal: \"\n         &lt;&lt; floatVal &lt;&lt; \"\\n\";\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Floating point literal: 4.14\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-character-literal\">3) Character Literal<\/h2>\n\n\n\n<p>Character literals are used to store a single character enclosed within single quotes (&#8221;). To store multiple characters, you would typically use a character array. Attempting to store more than one character within single quotes results in a warning, and only the last character of the literal is considered.<\/p>\n\n\n\n<p>There are two primary representations of character literals:<\/p>\n\n\n\n<p>A. <strong>char Type:<\/strong> This representation is used to store normal character literals or narrow-character literals. It is supported by both C and C++. Character literals of this type can hold a single character within single quotes and are widely used in both languages.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ For C\nchar chr = 'G';\n\n\/\/ For C++\nchar chr = 'G';<\/code><\/pre>\n\n\n\n<p>B. <strong>wchar_t type:<\/strong> Unlike the &#8216;char&#8217; type, &#8216;wchar_t&#8217; is used to represent wide-character literals and is supported only in C++. When a character is followed by the &#8216;L&#8217; prefix, it indicates that the literal should be stored as a &#8216;wchar_t,&#8217; representing a wide-character literal. This is particularly useful for dealing with wide characters and character sets that require a larger storage space than regular narrow characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Not Supported For C\n\n\/\/ For C++\nwchar_t chr = L'G';<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n  \nint main()\n{\n    \/\/ constant char literal\n    const char charVal = 'A';\n  \n    printf(\"Character Literal: %c\\n\",\n        charVal);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Character Literal: A<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-string-literals\">4) String Literals<\/h2>\n\n\n\n<p>String literals indeed serve as containers for multiple characters and are enclosed within double quotes (&#8220;&#8221;). Unlike character literals, string literals can store sequences of characters, making them suitable for representing text and longer data.<\/p>\n\n\n\n<p>String literals can also handle special characters and escape sequences, which are denoted by backslashes, such as &#8216;\\n&#8217; for a newline or &#8216;\\t&#8217; for a tab. This flexibility allows you to include various characters and control codes within a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char greeting&#91;] = \"Hello, World!\";<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    \/\/ String literal\n    char greeting&#91;] = \"Hello, World!\";\n\n    \/\/ Printing the string literal\n    printf(\"Message: %s\\n\", greeting);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<p>Hello, World!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-boolean-literals\">5) Boolean Literals<\/h2>\n\n\n\n<p>.Boolean literals are specific to C++ and are used to represent boolean data types. These literals can have two values:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>true:<\/strong> This represents the &#8220;True&#8221; value and should not be considered equal to the integer 1, as it&#8217;s a distinct boolean value.<\/li>\n\n\n\n<li><strong>false:<\/strong> This represents the &#8220;False&#8221; value and should not be considered equal to the integer 0, as it&#8217;s a distinct boolean value.<\/li>\n<\/ol>\n\n\n\n<p>Boolean literals are essential for writing clear and unambiguous code in C++ when working with boolean data types, making the code more expressive and readable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ program to show Boolean literals\n  \n#include &lt;iostream&gt;\nusing namespace std;\n  \nint main()\n{\n    const bool isTrue = true;\n    const bool isFalse = false;\n  \n    cout &lt;&lt; \"isTrue? \"\n        &lt;&lt; isTrue &lt;&lt; \"\\n\";\n    cout &lt;&lt; \"isFalse? \"\n        &lt;&lt; isFalse &lt;&lt; \"\\n\";\n  \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>isTrue? 1\nisFalse? 0\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-literals-in-c-c-with-examples\">FAQ- Literals In C\/C++ With Examples<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695117248604\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is literals with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Literals are indeed constant values that are directly embedded in a program&#8217;s code and can be assigned to variables. Your example, &#8220;int count = 0;&#8221;, demonstrates this concept effectively.<br \/>In this statement, &#8220;int count&#8221; is the declaration of an integer variable named &#8216;count,&#8217; and the literal &#8216;0&#8217; directly represents the value assigned to this variable, which, in this case, is zero. Literals are a fundamental part of programming, helping to initialize variables and express fixed values within the code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695117313584\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How are literals stored in c?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.String literals are indeed stored in the program&#8217;s memory as a sequence of characters, and they are terminated by a null character (&#8216;\\0&#8217;) with an ASCII value of 0. This null character marks the end of the string, allowing functions that operate on strings to determine where the string ends.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695117437193\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Which are examples of numeric literals?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.Examples for Numeric literals are \u00a0<strong>1 , .<\/strong>\u00a0<strong>2 , 3.4 , -5 , -6.78 , +9.10<\/strong>\u00a0. Approximate-value numeric literals are represented in scientific notation with a mantissa and exponent.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Literals In C\/C++ With Examples Literals in the C and C++ programming languages are fundamental building blocks that represent constant values directly in code. These constants can include integers, floating-point numbers, characters, and more. Literals are essential for initializing variables, specifying values in expressions, and conveying data directly within the source code. They provide a &#8230; <a title=\"Literals In C\/C++ With Examples\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/literals-in-c-c-with-examples\/\" aria-label=\"More on Literals In C\/C++ With Examples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5296,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,27],"tags":[335],"class_list":["post-1972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp-programming","category-c-programming","tag-literals-in-c-c-with-examples","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=1972"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1972\/revisions"}],"predecessor-version":[{"id":10617,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1972\/revisions\/10617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5296"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=1972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=1972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=1972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}