Home | | Computer Science 11th std | Literals (Constants) - C++ program

# Literals (Constants) - C++ program

Literals are data items whose values do not change during the execution of a program.

Literals (Constants)

Literals are data items whose values do not change during the execution of a program. Therefore Literals are called as Constants. C++ has several kinds of literals:

Numeric Constants:

As the name indicates, the numeric constants are numeric values, which are used as constants. Numeric constants are further classified as:

• Integer Constants (or) Fixed point constants.

• Real constants (or) Floating point constants.

## (1) Integer Constants (or) Fixed point constants

Integers are whole numbers without any fractions. An integer constant must have at least one digit without a decimal point. It may be signed or unsigned. Signed integers are considered as negative, commas and blank spaces are not allowed as part of it. In C++, there are three types of integer constants: (i) Decimal (ii) Octal (iii) Hexadecimal

### (i) Decimal

Any sequence of one or more digits (0 …. 9)

If you assign 4.56 as an integer decimal constant, the compiler will accept only the integer portion of 4.56 ie. 4. It will simply ignore .56.

If a Decimal constant declared with fractions, then the compiler will take only the integer part of the value and it will ignore its fractional part. This is called as “Implicit Conversion”. It will be discussed later.

### (ii) Octal

Any sequence of one or more octal values (0 …. 7) that begins with 0 is considered as an Octal constant.

When you use a fractional number that begins with 0, C++ has consider the number as an integer not an Octal.

Any sequence of one or more Hexadecimal values (0 …. 9, A …. F) that starts with 0x or 0Xis considered as an Hexadecimal constant.

The suffix L or l and U or u added with any constant forces that to be represented as a long or unsigned constant respectively.

## (2) Real Constants (or) Floating point constants

A real or floating point constant is a numeric constant having a fractional component. These constants may be written in fractional form or in exponent form.

Fractional form of a real constant is a signed or unsigned sequence of digits including a decimal point between the digits. It must have at least one digit before and after a decimal point. It may have prefix with + or - sign. A real constant without any sign will be considered as positive.

Exponent form of real constants consists of two parts: (1) Mantissa and (2) Exponent. The mantissa must be either an integer or a real constant. The mantissa followed by a letter E or e and the exponent. The exponent should also be an integer.

For example, 58000000.00 may be written as 0.58 × 108 or 0.58E8.

Example:

## Boolean Literals

Boolean literals are used to represent one of the Boolean values(True or false). Internally true has value 1 and false has value 0.

## Character constant

A character constant is any valid single character enclosed within single quotes. A character constant in C++ must contain one character and must be enclosed within a single quote.

Valid character constants : ‘A’, ‘2’, ‘\$’

Invalid character constants : “A”

The value of a single character constant has an equivalent ASCII value. For example, the value of ‘A’ is 65.

## Escape sequences (or) Non-graphic characters

C++ allows certain non-printable characters represented as character constants. Non-printable characters are also called as non-graphical characters. Non-printable characters are those characters that cannot be typed directly from a keyboard during the execution of a program in C++, for example: backspace, tabs etc. These non-printable characters can be represented by using escape sequences. An escape sequence is represented by a backslash followed by one or two characters.

### Table 9.2 Escape Sequences

Even though an escape sequence contains two characters, they should be enclosed within single quotes because, C++ consider escape sequences as character constants and allocates one byte in ASCII representation.

ASCII (American Standard Code for Information Interchange) was first developed and published in 1963 by the X3 committee, a part of the American Standards Association (ASA).

## String Literals

Sequence of characters enclosed within double quotes are called as String literals. By default, string literals are automatically added with a special character ‘\0’ (Null) at the end. Therefore, the string “welcome” will actually be represented as “welcome\0” in memory and the size of this string is not 7 but 8 characters i.e., inclusive of the last character \0.

Valid string Literals : “A”, “Welcome” “1234”

Invalid String Literals : ‘Welcome’, ‘1234’

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 9 : Introduction to C++ : Literals (Constants) - C++ program |