Numeric, boolean, and pointer literals (C++) (2024)

  • Article

A literal is a program element that directly represents a value. This article covers literals of type integer, floating-point, boolean, and pointer. For information about string and character literals, see String and Character Literals (C++). You can also define your own literals based on any of these categories. For more information, see User-defined literals (C++).

You can use literals in many contexts, but most commonly to initialize named variables and to pass arguments to functions:

const int answer = 42; // integer literaldouble d = sin(108.87); // floating point literal passed to sin functionbool b = true; // boolean literalMyClass* mc = nullptr; // pointer literal

Sometimes it's important to tell the compiler how to interpret a literal, or what specific type to give to it. It's done by appending prefixes or suffixes to the literal. For example, the prefix 0x tells the compiler to interpret the number that follows it as a hexadecimal value, for example 0x35. The ULL suffix tells the compiler to treat the value as an unsigned long long type, as in 5894345ULL. See the following sections for the complete list of prefixes and suffixes for each literal type.

Integer literals

Integer literals begin with a digit and have no fractional parts or exponents. You can specify integer literals in decimal, binary, octal, or hexadecimal form. You can optionally specify an integer literal as unsigned, and as a long or long long type, by using a suffix.

When no prefix or suffix is present, the compiler will give an integral literal value type int (32 bits), if the value will fit, otherwise it will give it type long long (64 bits).

To specify a decimal integral literal, begin the specification with a nonzero digit. For example:

int i = 157; // Decimal literalint j = 0198; // Not a decimal number; erroneous octal literalint k = 0365; // Leading zero specifies octal literal, not decimalint m = 36'000'000 // digit separators make large values more readable

To specify an octal integral literal, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal literal. For example:

int i = 0377; // Octal literalint j = 0397; // Error: 9 is not an octal digit

To specify a hexadecimal integral literal, begin the specification with 0x or 0X (the case of the "x" doesn't matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:

int i = 0x3fff; // Hexadecimal literalint j = 0X3FFF; // Equal to i

To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. To specify a 64-bit integral type, use the LL, or ll suffix. The i64 suffix is still supported, but we don't recommend it. It's specific to Microsoft and isn't portable. For example:

unsigned val_1 = 328u; // Unsigned valuelong val_2 = 0x7FFFFFL; // Long value specified // as hex literalunsigned long val_3 = 0776745ul; // Unsigned long valueauto val_4 = 108LL; // signed long longauto val_4 = 0x8000000000000000ULL << 16; // unsigned long long

Digit separators: You can use the single-quote character (apostrophe) to separate place values in larger numbers to make them easier for humans to read. Separators have no effect on compilation.

long long i = 24'847'458'121;

Floating point literals

Floating-point literals specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.

Floating-point literals have a significand (sometimes called a mantissa), which specifies the value of the number. They have an exponent, which specifies the magnitude of the number. And, they have an optional suffix that specifies the literal's type. The significand is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:

18.4638.

The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:

18.46e0 // 18.4618.46e1 // 184.6

The exponent may be specified using e or E, which have the same meaning, followed by an optional sign (+ or -) and a sequence of digits. If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.

Floating-point literals default to type double. By using the suffixes f or l or F or L (the suffix isn't case sensitive), the literal can be specified as float or long double.

Although long double and double have the same representation, they're not the same type. For example, you can have overloaded functions such as

void func( double );

and

void func( long double );

Boolean literals

The boolean literals are true and false.

Pointer literal (C++11)

C++ introduces the nullptr literal to specify a zero-initialized pointer. In portable code, nullptr should be used instead of integral-type zero or macros such as NULL.

Binary literals (C++14)

A binary literal can be specified by the use of the 0B or 0b prefix, followed by a sequence of 1's and 0's:

auto x = 0B001101 ; // intauto y = 0b000001 ; // int

Avoid using literals as "magic constants"

You can use literals directly in expressions and statements although it's not always good programming practice:

if (num < 100) return "Success";

In the previous example, a better practice is to use a named constant that conveys a clear meaning, for example "MAXIMUM_ERROR_THRESHOLD". And if the return value "Success" is seen by end users, then it might be better to use a named string constant. You can keep string constants in a single location in a file that can be localized into other languages. Using named constants helps both yourself and others to understand the intent of the code.

See also

Lexical conventions
C++ string literals
C++ user-defined literals

Numeric, boolean, and pointer literals (C++) (2024)
Top Articles
'Nah, imagine working for Walmart for 35 years': Walmart worker shames employees who stay with the company for decades
Work ethic: What goes into a great one?
Netronline Taxes
What Are Romance Scams and How to Avoid Them
Ups Dropoff Location Near Me
Kaydengodly
Is pickleball Betts' next conquest? 'That's my jam'
Atvs For Sale By Owner Craigslist
Fort Carson Cif Phone Number
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
craigslist: south coast jobs, apartments, for sale, services, community, and events
Simple Steamed Purple Sweet Potatoes
Thotsbook Com
Drago Funeral Home & Cremation Services Obituaries
Scenes from Paradise: Where to Visit Filming Locations Around the World - Paradise
Wal-Mart 140 Supercenter Products
Prestige Home Designs By American Furniture Galleries
Tamilyogi Proxy
Sprinkler Lv2
Drift Boss 911
Juicy Deal D-Art
Noaa Duluth Mn
Mybiglots Net Associates
Jordan Poyer Wiki
Foodsmart Jonesboro Ar Weekly Ad
1979 Ford F350 For Sale Craigslist
Craigslist Rentals Coquille Oregon
New Stores Coming To Canton Ohio 2022
Pokémon Unbound Starters
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Ncal Kaiser Online Pay
Schooology Fcps
Motor Mounts
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
Puerto Rico Pictures and Facts
One Credit Songs On Touchtunes 2022
Selfservice Bright Lending
Heavenly Delusion Gif
Hisense Ht5021Kp Manual
Admissions - New York Conservatory for Dramatic Arts
Main Street Station Coshocton Menu
Oriellys Tooele
Jack In The Box Menu 2022
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Ronnie Mcnu*t Uncensored
El Patron Menu Bardstown Ky
Cryptoquote Solver For Today
Black Adam Showtimes Near Kerasotes Showplace 14
2487872771
Overstock Comenity Login
E. 81 St. Deli Menu
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6325

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.