C++ Constants and Literals - Great Learning (2024)

In this section, we will find out more on C++ constant and literals. In C++, const modifier is used to create constants i.e. variables whose value cannot be changed once they are initialized. Generally constants are written in CAPITAL and usually placed in the top of the program file. Its syntax is as follows:

const type variable = value;const int PI = 3.14;

You cannot reset the value of a constant. For e.g. the following code snippet will give error as we are trying to change a constant literal's value.

#include <iostream>using namespace std;int main(){ int i,j; .... const int SPEED_OF_LIGHT = 300000000; ... SPEED_OF_LIGHT = 250000000 // Error! Cannot change SPEED_OF_LIGHT which is a constant. ...}

Another way to creare a constant is using the #define preprocessor directive. For e.g.

# define SPEED_OF_LIGHT 300000000

C++ literals are data used for representing fixed point values that we can directly use in the code. For example: 100, 3.1421, 'y', 'n' etc.

Please note that a variable can be assign different values but literals can’t. There are 6 types of literals in C++.

1. Integer literal 🡪 An integer is a fixed point numeric literal. It has no fractional or exponential part. There are three types of integer literals used in C++ programming i.e. decimal (base 10), octal (base 8) and hexadecimal (base 16). Please note that octal literal starts with a 0 while hexadecimal literal starts with a 0x. For e.g.

Decimal literal : -1, 0, 100 etc.
Octal literal : 045, 067, 043 etc.
Hexadecimal literal: 0x8f, 0xa2, 0x721 etc.

Floating point literal 🡪 A floating-point literal is different from integar literal as it has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point

B.literals either in decimal form or exponential form. Decimal form must have the decimal point, the exponent, or both and while the exponential form must have the integer part, the fractional part, or both. The signed exponent is denoted by e or E. For e.g.

3.14134 // Legal314159E-5L // Legal620E // Illegal: incomplete exponent22f // Illegal: no decimal or exponent.e755 // Illegal: missing integer or fraction

C.Boolean literal 🡪 C++ has two Boolean literals i.e. true which denotes for success/truth and false which stands for failure/falsehood. A programmer usually uses considers the value of true equal to 1 and value of false equal to 0.

D.Character literal 🡪 A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc. Please note that f for literals begins with L, you should store it in wchar_t type of variable as it is a wide character literal. For e.g.

'y', 'n', L'fu', '(', ']' etc.

E.String literals 🡪 String literals are proper strings that are enclosed in double quotes ("...."). A string literal can have anything such as plain characters, escape sequences, and universal characters. We can break a long line into multiple lines using string literals and separate them using whitespaces. Here are some examples of string literals that are identical strings.

"India is great""India \is great""India \is \great"

F.Escape sequence 🡪 this type of literal is used for some special operation or to denote a special character that cannot be typed using keyboard. For example, newline (enter), tab, question mark, etc. Some common escape sequences are \n to add new line; \t to add tab space; \b to add a backspace etc.

C++ Constants and Literals - Great Learning (2024)
Top Articles
Wells Fargo CDs: Pros, Cons, and Current Rates
Understanding Group Life Insurance
Davita Internet
Ffxiv Palm Chippings
Research Tome Neltharus
Valley Fair Tickets Costco
Mohawkind Docagent
Emmalangevin Fanhouse Leak
Mndot Road Closures
Erskine Plus Portal
13 The Musical Common Sense Media
World Cup Soccer Wiki
Craigslist Heavy Equipment Knoxville Tennessee
Slag bij Plataeae tussen de Grieken en de Perzen
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Love In The Air Ep 9 Eng Sub Dailymotion
Leader Times Obituaries Liberal Ks
Committees Of Correspondence | Encyclopedia.com
Huntersville Town Billboards
Timeforce Choctaw
Ford F-350 Models Trim Levels and Packages
Routing Number For Radiant Credit Union
Sofia the baddie dog
City Of Durham Recycling Schedule
Urbfsdreamgirl
Truvy Back Office Login
Table To Formula Calculator
Sandals Travel Agent Login
Orange Park Dog Racing Results
Neteller Kasiinod
Maths Open Ref
DIY Building Plans for a Picnic Table
Otis Offender Michigan
Have you seen this child? Caroline Victoria Teague
The Pretty Kitty Tanglewood
Steven Batash Md Pc Photos
Tamil Play.com
Atlantic Broadband Email Login Pronto
Spinning Gold Showtimes Near Emagine Birch Run
Oreillys Federal And Evans
Asian Grocery Williamsburg Va
Afspraak inzien
Directions To 401 East Chestnut Street Louisville Kentucky
Academic important dates - University of Victoria
Gpa Calculator Georgia Tech
Housing Intranet Unt
T&Cs | Hollywood Bowl
St Vrain Schoology
Online College Scholarships | Strayer University
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
Unpleasant Realities Nyt
Tyrone Unblocked Games Bitlife
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5826

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.