Constants / Literals in C++





C++ Constants :

  • Constants are named locations in memory that are used to hold a value that cannot be modified by the program.

  • Constants remain unchanged during the execution of a program.

  • const keyword is used to declare constant data.

Syntax to declare constants :

const data_type identifier = value;

EXAMPLE:
const float PI = 3.14f;

Type of constants in C++ :

    C++ constants can be divided into two major categories :

   1. Primary Constants
   2. Secondary Constants

Literals :

  • A C++ literal is a sequence of characters (digits, letters, and other characters) that represents constant value to be stored in a variable.

  • C++ specifies five major types of literals :

  1. Integer Literals

  2. Floating-Point Literals

  3. Character Literals

  4. String Literals

  5. Boolean Literals


Integer Literals :

There are three ways to represent integer number in the C++ language :
  1. Decimal ( base 10 )

    int x = 20;

  2. Octal ( base 8 )

    int x = 076; (starts with 0)

  3. Hexadecimal ( base 16 )

    int x = 0x13A; (starts with 0x)


Floating-Point Literals :

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

Example

3.143
23123E-2L


Character Literals :

  • A character literal is represented by a single character in single quotes.

    Example:

    char c = 'a';
    char x = '@';

  • If the literal begins with L (uppercase only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of variable .

  • A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a unicode character (starts with \u) (e.g., '\u02C0').

  • C++allows us to have certain non graphic characters in character constants.Non graphic characters are those characters that cannot be typed directly from keyboard, for example, tabs, carriage return, etc.

  • These non graphic characters can be represented by using escape sequences represented by a backslash(\) followed by one or more characters.

    Escape Sequence           Description    
         \a     Alert
         \b     Backspace
         \f     Formfeed
         \n     Newline
         \r     Carriage return
         \t     Horizontal tab
         \v     Vertical tab
         \\     Backslash
         \"     Double quotation
         \'     Single quotation
         \?     Question Mark

    Example :

    #include < iostream >
    int main()
    {
    cout <<"Hello\nfrom programe.";
    return(0);
    }
    
    OUTPUT:
    Hello
    from programe.
    

String Literals :

String literals are enclosed in double quotes.

Example :

"hello world"
"C++ prowess"

Boolean Literals :

There are two Boolean literals and they are part of standard C++ keywords:

  • true
  • false

Example :

bool b = true;
bool b = false;

Next topic is Storage Classes in C++





 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved