Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Constant
1.2.
Literals
2.
String Literals
2.1.
Example
3.
Character Literals
3.1.
 
3.2.
 
3.3.
 
3.4.
 
3.5.
 
3.6.
 
3.7.
 
3.8.
Example
4.
Boolean Literals
4.1.
true
4.2.
false 
4.3.
Example
5.
Integer Literals
5.1.
Decimal number (base 10)
5.2.
Octal number (base 8)
5.3.
Hexadecimal number (base 16)
5.4.
Example
6.
Floating-point Literals
6.1.
Example
7.
Defining Constants:
7.1.
The #define preprocessor
7.2.
Example
7.3.
The const keyword
7.4.
Example
8.
Frequently Asked Questions
9.
Key Takeaways
Last Updated: Mar 27, 2024

Constants/Literals

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Constant

A constant is a variable whose value can not be changed. It's useful to think of constants as packing containers that hold records that cannot be modified later.

You could consider constants a bag to shop for some books that can not be replaced as soon as placed in the bag.

Literals

Literals are hardcoded values, including the range 5, a man or a woman, or the string, "good day, world!" Numeric literals may be represented in the diffusion of formats (decimal, hexadecimal, binary, floating-point, and so on). A literal always represents the equal value (5 always represents the quantity of 5).

There are five primary varieties of literals:

  1. String Literals
  2. Character Literals
  3. Boolean Literals
  4. Integer Literals
  5. Floating-point Literals

String Literals

A "string literal" is a sequence of characters from the source individual set enclosed in double quotation marks (" "). String literals are used to symbolize a series of characters that, taken togetherform a null-terminated string. You must constantly prefix wide-string literals with the letter L.

Example

String1= "CodingNinjas";

String2= "family";

For the above two strings, we use the '+' operator, as shown in the below statement:

"CodingNinjas" + "family"=” CodingNinjasfamily”


Also, see Literals in C. Fibonacci Series in C++

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Character Literals

This refers to the literals that are used to save a single character within a single quote. To store multiple characters, one needs to apply a character array. Storing multiple characters within a single quote will throw a warning and display just the last character of the literal.

There are certain characters in C++; while a backslash precedes them, they will have a unique meaning, and they're used to symbolize like newline (n) or tab (t). Following is the list of some escape characters:

Escape sequence               Meaning
\\ \ character
\" " character
\' ' character
\a Alert or bell
\? ? character
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

 

 

 

 

 

 

 

 

You can also do a free certification of Basics of C++.

Example


#include <iostream>
using namespace std;
int main() {
   cout << "Coding\tNinjas\n";
   return 0;
}


Output:

Coding    Ninjas


Also see, Abstract Data Types in C++

Boolean Literals

This literal is furnished simplest in C++ and not in C. They represent the boolean data types as either True or False

true

to represent true value. This should now not be considered equal to int 1.

false 

to represent false value. This ought to no longer be considered the same as int 0.

Example

#include <iostream>
using namespace std;
  
int main()
{
    const bool isTrue = true;
    const bool isFalse = false;
  
    cout << "isTrue? "
         << isTrue << "\n";
    cout << "isFalse? "
         << isFalse << "\n";
  
    return 0;
}


Output:

isTrue? 1
isFalse? 0

You can try and compile with the help of online c++ compiler.

Integer Literals

It's a numeric literal that represents only integer kind values. It represents the value neither in fractional nor exponential components. It may be distinctive within the following three methods:

Decimal number (base 10)

It can be described through representing the digits among 0 to 9. for example, 45, 67, etc.

Octal number (base 8)

It is defined as a number wherein 0 is accompanied by digits consisting of 0,1,2,3,4,5,6,7. For Example 012, 034, 0.5, etc.

Hexadecimal number (base 16)

It is described as a variety in which 0x or 0X is observed via the hexadecimal digits (i.e., digits from 0 to 9, alphabetical characters from (a-z) or (A-Z)).

For example:

Decimal: 0, -9, 22 etc

Octal: 021, 077, 033 etc

Hexadecimal: 0x7f, 0x2a, 0x521 etc

Example

include <iostream>
using namespace std;
  
int main()
{
    const int intVal = 20;
    cout << "Integer Literal: "
         << intVal << "\n";
  
    return 0;
}


Output:

Integer Literal:20

Floating-point Literals

Floating-point Literals are used to represent and save real numbers. The real number has an integer element, an actual element, a fractional element, and an exponential element. The floating-factor literals can be stored both in decimal form or exponential form. While representing the floating-factor decimals, one should hold two matters in mind to supply valid literals:

  • In the decimal form, one should include the decimal point, exponent component, or both. In any other case, it will result in any errors.
  • In the exponential shape, one needs to include the integer element, fractional part, or both.  otherwise, it'll cause an error.

Example

#include <iostream>
using namespace std;
  
int main()
{
    const float floatVal = 4.14;
  
    cout << "Floating-point literal: "
         << floatVal << "\n";
    return 0;
}


Output:

Floating-point literal: 4.14


Also Read - C++ Interview Questions

Defining Constants:

In the C/C++ program, we will define constants in methods as shown below:

  • Using “#define” preprocessor.
  • Using “const” keyword.

The #define preprocessor

Syntax : #define identifierName value

  1.   identifierName: it is the name given to constant.
  2.   value: This refers to any value assigned to identifierName.

Example

#include <iostream>
using namespace std;

#define VALUE1 15  
#define VALUE2 16

int main()
{
   int TOTAL; 
   TOTAL = VALUE1 * VALUE2;
   cout << TOTAL;
   cout << endl;
}


Output:

240

The const keyword

Syntax: const type constant_name;

The usage of the const keywords to define constants is as simple as defining variables. The difference is you'll precede the definition with the const keyword.

Example

#include <iostream>
using namespace std;


int main()
{
  const int L = 15;
  const int B = 15;
  int area;
  area = L*B;
  cout<<"The area of the square " << L<<"x"<<B <<" is: " << area << endl;
  return 0;
}


Output:

The area of the square 15x15 is: 225

Frequently Asked Questions

  1. How are the constants declared?
    The const will declare with a specific type value and,  #define used to declare user-defined constants.
     
  2. Number systems whose digits consist of the numbers 0 through 9 are called as?
    Decimal Number System
     
  3. Integer literal containing value "F" comes under?
    Hexa Decimal Number

Key Takeaways

We have learned about constants, literals, and the differences between them. We also learned how to use constants and literals in the programming language and learned about kinds of literals. For more practice, to ace technical interviews, you can visit here.

Previous article
Variables
Next article
Size of Array in C++
Live masterclass