Programming languages - C++

Back to Course

Lesson Description

Lession - #38 C++ Constants/Literals

Constants allude to fixed values that the program may not modify and they are called literals.

Constants can be of any of the essential data types and can be separated into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Once more, constants are dealt with very much like ordinary variables aside from that their qualities can't be altered after their definition.

Integer Literals

A integer literal can be a decimal, octal, or hexadecimal constant. A prefix indicates the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

A integer literal can likewise have a postfix that is a mix of U and L, for unsigned and long, separately. The postfix can be capitalized or lowercase and can be in any request.

Here are a few instances of integer literals −

212         // Legal
215u        // Legal
0xFeeL      // Legal
078         // Illegal: 8 is not an octal digit
032UU       // Illegal: cannot repeat a suffix



Floating-point Literals

A floating-point literal has a integer section, a decimal point, a fractional part, and an exponent part. You can address floating point literals either in decimal structure or exponential structure.

While addressing utilizing decimal structure, you should incorporate the decimal point, the exponent, or both and keeping in mind that addressing utilizing exponential structure, you should incorporate the whole integer part, the fractional part, or both. The marked type is presented by e or E.

Here are a few instances of floating point literals −





3.14159       // Legal
314159E-5L    // Legal
510E          // Illegal: incomplete exponent
210f          // Illegal: no decimal or exponent
.e55          // Illegal: missing integer or fraction





Boolean Literals

There are two Boolean literals and they are important for standard C++ keywords −



  • A value of true addressing true .
  • A value of false addressing false .

You shouldn't think about the value of genuine equivalent to 1 and value of false equivalent to 0.

Character Literals

Character literals are encased in single quotes. In the event that the literal starts with L (capitalized just>
, it is a wide character literal (e.g., L'x'>
and ought to be put away in wchar_t kind of variable . In any case, it is a limited character literal (e.g., 'x'>
and can be put away in a straightforward variable of char type.

A character literal can be a plain character (e.g., 'x'>
, a escape sequence (e.g., '\t'>
, or a universal character (e.g., '\u02C0'>

Following is the example to show a few escape sequence characters −

Live Demo
using namespace std;

int main(>
{ cout << "Hello\tWorld\n\n"; return 0; }



Defining Constants

There are two simple ways in C++ to define constants −



  • Using #define preprocessor.
  • Using const keyword.


The #define Preprocessor

Following is the form to use #define preprocessor to define a constant −



#define identifier value




The const Keyword

You can use const prefix to declare constants with a specific type as follows −




const type variable = value;

Following example explains it in detail −


using namespace std;

int main(>
{ const int LENGTH = 10; const int WIDTH = 5; const char NEWLINE = '\n'; int area; area = LENGTH * WIDTH; cout << area; cout << NEWLINE; return 0; }






There are 4 major oop principles c++ that make an language Object Oriented. These are Encapsulation, Data Abstraction, Polymorphism and Inheritance







sleep MSVC is a CRT function, and Sleep is a Windows API



OOPS Concepts In C# — Object-Oriented Programming is a programming model that works on a principle that revolves around objects rather than action or logic.



c plus plus object oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.



The Borland C++ Compiler 5.5 (BCC>
is a blazingly fast 32-bit optimizing compiler.