Introduction to C + +: Literals: Integer, Character and Floating Constant, String Literals

Doorsteptutor material for UGC Environmental-Science is prepared by world's top subject experts: fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 200K)

Literals

  • Literals (often referred to as constants) are data items that never change their value during the execution of the program.

  • The following types of literals are available in C++.

Image of Literals

Image of Literals

Integer Constants

  • Integer constants are whole numbers without any fractional part.

  • It may contain either + or – sign, but decimal point or commas does not appear in any integer constant.

  • C++ allows three types of integer constants.

    • Decimal (Base 10)

    • Octal (Base 8)

    • Hexadecimal (Base 16)

    • Decimal (Base 10)

  • It consists of sequence of digits and should not begin with 0 (zero). For example, 124, - 179, + 108.

  • Octal integer constants

  • It consists of sequence of digits starting with 0 (zero). For example, 014, 012.

  • Hexadecimal integer constant

  • It consists of sequence of digits preceded by ox or OX. For example, OXD, OXC. The suffix l or L and u or U attached to any constant forces it to be represented as a long and unsigned respectively.

Character-Constants

  • A character constant in C++ must contain one or more characters and must be enclosed in single quotation marks.

  • For example, ‘A’, ‘9’, etc. C++ allows nongraphic characters which cannot be typed directly from keyboard, e.g., backspace, tab, carriage return etc.

  • These characters can be represented by using a escape sequence.

  • An escape sequence represents a single character

  • The following table gives a listing of common escape sequences.

Image Character Constants

Image Character Constants

Floating Constants

  • They are also called real constants. They are numbers having fractional parts. They may be written in fractional from or exponent form

  • A real constant in fractional form consists of signed or unsigned digits including a decimal point between digits. For example, 3.0, -17.0, -0.627 etc.

String Literals

  • A sequence of character enclosed within double quotes is called a string literal.

  • String literal is by default (automatically) added with a special character ‘\O’ which denotes the end of the string.

  • Therefore, the size of the string is increased by one character. For example, “COMPUTER” will be represented as “COMPUTER\O” in the memory and its size is 9 characters.

Developed by: