C++ Constants

A constant are identifiers whose value does not change during program execution. Constants are sometimes referred to as literal. A constant or literal my be any one of the following:

Integer Constant
       An integer constant is a whole number which can be either positive or negative.  They do not have fractional part or exponents. We can specify integer constants in decimal, octal or hexadecimal form. Decimal Integer Constant: It consists of any combination of digits taken from the set 0 to 9.
example
int a = 100; //Decimal Constant
int b = -145 // A negative decimal constant
int c = 065 // Leading zero specifies octal constant, not decimal.

Octal Integer Constant
            It consists of any combination of digits taken from the set 0 to 7. However the first digit must be 0, in order to identify the constant as octal number.
example
int a = 0374; //Octal Constant
int b = 097; // Error: 9 is not an octal digit.

 Hexadecimal Integer Constant
          A Sequence of digits begin the specification with 0X or 0x, followed by a sequence of digits in the range 0 to 9 and A (a) to F(f).
example
int a = 0x34;
int b = -0XABF;

Unsigned Constant
            To specify an unsigned type, use either u or U suffix. To specify a long type, use either the l or L suffix.
example
unsigned a = 328u; //Unsigned value
long b = 0x7FFFFFL; //Long value specified as hex constant
unsigned long c = 0776745ul; //Unsigned long values as octal constant

Floating Point Constant
                 Floating point constants are also called as “real constants”.  These values contain decimal points (.) and can contain exponents They are used to represent values that will have a fractional part and can be represented in two forms (i.e. fractional  form and exponent form) Floating-point constants have a “mantissa”, which specifies the value of the number, an “exponent” which specifies  magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. The exponent, if present, specifies the magnitude of the number as a power of 10.

 Example: 23.46e0 // means it is equal to 23.46 x 100 = 23.46 x 1 = 23.46

Character Constants 
         Character constants are specified as single character enclosed in pair of single quotation marks. For example char ch = ‘P’; //Specifies normal character constant A single character constant such as ‘D’ or ‘r’ will have char data type. These character constants will be assigned numerical values. The numerical values are the ASCII values which are numbered sequentially for both uppercase and lowercase letters.

String Constants
        A string constant consists of zero or more character enclosed by double quotation marks (“). Multiple character constants are called string constants and they are treated as an array of char. By default compiler adds a special character called the “Null Character” (\0) at the end of the string to mark the end of the string.

C++ Constants C++ Constants Reviewed by Unknown on 04:30 Rating: 5

No comments:

Powered by Blogger.