Examples for integral constants

Examples for integral constants

 

Note 
In each line of the above table, the same value is presented in a different way.

Sample program

// To display hexadecimal integer literals and
// decimal integer literals.
//
#include <iostream>
using namespace std;
int main()
{
  // cout outputs integers as decimal integers:
  cout << "Value of 0xFF = " << 0xFF << " decimal"
       << endl;                 // Output: 255 decimal
  // The manipulator hex changes output to hexadecimal
  // format (dec changes to decimal format):
  cout << "Value of 27 = " << hex << 27 <<" hexadecimal"
       << endl;              // Output: 1b hexadecimal
  return 0;
} 
The boolean keywords true and false, a number, a character, or a character sequence (string) are all constants, which are also referred to as a literals. Constants can thus be subdivided into
  • boolean constants
  • numerical constants
  • character constants
  • string constants.
Every constant represents a value and thus a type—as does every expression in C++. The type is defined by the way the constant is written.

Boolean Constants

A boolean expression can have two values that are identified by the keywords true and false. Both constants are of the bool type. They can be used, for example, to set flags representing just two states.

Integral Constants

Integral numerical constants can be represented as simple decimal numbers, octals, or hexadecimals:
  • a decimal constant (base 10) begins with a decimal number other than zero, such as 109 or 987650
  • an octal constant (base 8) begins with a leading 0, for example 077 or 01234567
  • a hexadecimal constant (base 16) begins with the character pair 0x or 0X, for example 0x2A0 or 0X4b1C. Hexadecimal numbers can be capitalized or non-capitalized.
Integral constants are normally of type int. If the value of the constant is too large for the int type, a type capable of representing larger values will be applied. The ranking for decimal constants is as follows:
int, long, unsigned long 
You can designate the type of a constant by adding the letter L or l (for long), or U or u (for unsigned). For example,
12L
and
12l
correspond to the type long
12U
and
12u
correspond to the type unsigned int
12UL
and
12ul
correspond to the type unsigned long 

Floating-Point Constants

Floating-point numbers are always represented as decimals, a decimal point being used to distinguish the fraction part from the integer part. However, exponential notation is also permissible.
Examples:
27.1     1.8E–2     // Type: double 
Here, 1.8E–2 represents a value of 1.8*10–2. E can also be written with a small letter e. A decimal point or E (e) must always be used to distinguish floating-point constants from integer constants.
Floating-point constants are of type double by default. However, you can add F or f to designate the float type, or add L or l for the long double type.

Character Constants

A character constant is a character enclosed in single quotes. Character constants take the type char.
Examples:
'A'     // Type: char 
The numerical value is the character code representing the character. The constant 'A' thus has a value of 65 in ASCII code.

String Constants

You already know string constants, which were introduced for text output using the cout stream. A string constant consists of a sequence of characters enclosed in double quotes.
Examples:
"Today is a beautiful day!" 
A string constant is stored internally without the quotes but terminated with a null character, \0, represented by a byte with a numerical value of 0 — that is, all the bits in this byte are set to 0. Thus, a string occupies one byte more in memory than the number of characters it contains. An empty string, "", therefore occupies a single byte.
The terminating null character \0 is not the same as the number zero and has a different character code than zero. Thus, the string
Examples:
"0"
comprises two bytes, the first byte containing the code for the character zero 0 (ASCII code 48) and the second byte the value 0.

Post a Comment

0 Comments