C++ Literals Detailed Explanation

In this tutorial, we will dive deeper into the details of C++ Literals and will provide you a complete overview of its types. Literals are data items that never change their value during the program run. Literals are also known as constant.

C++ Literals Detailed Explanation

In this tutorial, we will learn more about C++ Literals. We have already given a short description of C++ Literals in the last tutorial. Let us first recap that what we have studied about C++ Literals in our last tutorials then we will discuss more about the kinds of C++ Literals.

Literals are also called constant, these are data items that never change its value during the program run.

C++ allows several types of literals :

  1. Integer Literals
  2. Character Literals
  3. Floating Literals
  4. String Literals
  5. Boolean Literals
  6. Defining Literals

Let us discuss each type of literals one by one, So we will start from the integer-literals

Integer Literals 

Integer Literals are whole numbers without any decimal or fractional part. The method writing integer literals is that it must have at least one digit and it should not contain any decimal point. It can be positive ( + ) or negative ( - ). The default type is positive which means number without any sign is considered as positive. They cannot have commas in it.

C++ allows three types of integer literals :

  • Decimal ( base 10 )
  • Octal ( base 8 )
  • Hexadecimal ( base 16 )
  1. Decimal Integer Literals: An integer consisting of a sequence of digits is taken to be a decimal integer literals unless it begins with 0. Example 123, 46, +23, -43 etc.
  2. Octal Integer Literals: A sequence of digits starting with 0 is taken to be an octal integer. For example, 8 is written as 010 as an octal integer.
  3. Hexadecimal Integer Literals: A sequence of digits preceded by 0x or 0X is taken to be a hexadecimal integer. For example decimal, 12 will be written as 0XC as a hexadecimal integer.

Character Literals

A character constant is a single character enclosed in single quotes, like 'a'. The rule for writing character constant is that it must contain one character enclosed in single quotation marks.

Nongraphic characters can be represented using escape sequences. It is represented using a backslash (\) followed by one or more characters.

Escape Sequence 

 

Nongraphic Character

             \a  Audible Bell (alert)
             \b  Backspace
             \f  Formfeed
             \n  Newline or linefeed
             \r  Carriage Return
             \t  Horizontal Tab
             \v  Vertical Tab  
             \\  Backslash
              \'  Single quote
               \"  Double quote
              \?  Question Marks
              \On  Octal Number
              \xHn  Hexadecimal Number
              \o  Null

Floating Literal

Floating literals are also called real constants. Real constants are numbers having fractional parts. They can be written in one of the two forms called fractional form or the exponential form. Example 2.0, 17.5, -13.0, -0.00873, 125E05.

String Literals 

Multiple characters constant can be treated in two ways in C++. If enclosed in single quotes these are treated as character constants and if enclosed in double-quotes then they are treated as string literals.

"A string literal is a sequence of characters surrounded by double quotes."

Let's discuss more punctuators and operators also.

Punctuators

The following characters are used as punctuators in C++:

[ ] ( ) { } , ; :  * ... =  #

Uses of some punctuators are listed below:

Brackets [ ] : Opening and closing brackets indicate single and multidimensional array subscripts

Parentheses ( ) : This indiciate function call and function parameter.

Braces { } : It  indicates block of code.

Operators

These are tokens that trigger some computation when applied to variables and other objects in an expression.

Types of operators are :

  1. Unary Operator: These operators require one operator to operate upon. Examples are &, *, ++, -- , ! , etc.
  2. Binary Operator: These operators require two operands to operate upon.
    • Arithmetic Operators 
    • Shift Operators
    • Bitwise Operators
    • Logical Operators
    • Assignment Operators
    • Relational Operators
    • Component Selection Operators
    • Class member Operators
    • Conditional Operators.

Now, this the end of this tutorial we will look at our first program in C++ in the next tutorial.

What's Your Reaction?

like
1
dislike
0
love
1
funny
0
angry
0
sad
0
wow
1