Check The Programming Section
When we combine alphabets, numbers and special characters together it forms keywords and various types of identifiers. These identities further divide into various categories, such as variables, constants, functions, labels, and various other user-defined objects. These identifies may vary from one to more characters. Here, we will discuss about the constant identifiers and its various types.
A constant is an entity that doesn’t change during the execution of the program. When we write a real program not like Hello, World!, we normally do lots of calculations and their values are stored into computer memory. To make these values useful and retrieval for further processes, all such memory locations are given a name, which a human (a programmer) can remember and use these names to read these memory blocks. A memory block looks like a box and has the following properties:
A Name
A value and
An address
A name is given for a programmer to remember a memory block and each memory block has a value, which is taken as input from the user or directly given by (for constants) the programmer. And an address is used by the CPU of the computer to read from (only for constants) and write to that memory block. Constants refer to fixed values that the program cannot alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals. In C and C++, compiler allows us to specify various types constants and these are listed below:
At this stage we would restrict our discussion to only Primary Constants, namely, Integer, Real and Character constants (as listed above). In C/C++, there is another type of constants and identified as secondary constants and like primary there is also various types such as
Array
Pointer
Structure
Union
Enum
Class (in C++)
Let us see the details of each of these constants. For constructing these different types of constants certain rules have been laid down. These rules are as under:
These constants are represented with whole numbers. They require a minimum of 2 bytes and maximum of 4 bytes memory. Following list of rules needs to follow to construct an integer constants:
At least one digit (a whole number) is needed to represent an integer constant. For example, 2, 56, 78.
A decimal point within an integer constant is not allowed. For example, 2.45 is not categorised under this category.
An integer constant can be a positive or a negative number. If no sign precedes an integer constant it is assumed to be a positive. For example, -34, 56.
Special characters like commas or blanks are not allowed within an integer constant. For example, 67,89 and 56 8 are not valid constants.
The range of the integer constants depends on the data types.
An integer constant has various forms. Besides representing the integers in decimal, they can also be represented in octal or hexadecimal number systems. A hexadecimal constant must consist of a 0x followed by the constant in hexadecimal form. An octal constant begins with a 0. Here are some examples:
int hex = 0x80; //128 in decimal
int oct = 012; //10 in decimal
Real constants are often called floating-point constants. Real constants are very useful in programming languages to represent entities like height, length, width, distance. The real constants can be written in two forms:
Fractional Form
Exponential Form
Like integer constants, to construct a real constants following points we need to remember:
A real constant must have at least one digit and must have a decimal point attached to it. For example, 2.45, 5.07.
It could be either a positive or negative. If no sign is preceded, then the default sign will be positive. For example, -45.67, 78.90.
Special characters like commas or blanks are not allowed within a real constant. For example, 67,89.67 and 56 .80 are not valid constants.
If the real constant values are too small or too big, then exponential form of representation is used. It however doesn’t restrict us in any way from using exponential form of representation for other real constants. The exponential form of a real number is represents with a character ‘e’ and will represent in between a real value The exponential form of a real constants is represented in two parts:
Mantissa - the part appearing before ‘e’
Exponent - the part appearing after ‘e’
The following points need to remember, while constructing the exponential form of a real number:
The mantissa and the exponential part of a real number must be separated by a letter e. For example, 3.45e3, 34.78e12.
The mantissa part may have a positive or negative sign and if no sign is preceded the mantissa part, then it will be considered as positive. For example, +3.2e4, -4.5e2.
The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. For example, some valid exponent values are 3.5e4, 4.5-e3, 1.3+5. The exponent forms like 3.45e and 2.34-e are wrong.
A single character constant is represented with a single character. It can be represented with a single digit or single alphabet or single special symbol or a white space enclosed within a pair of single quote marks. For example, 'A', '2' and '+' are some valid single cahracter constnats.
Each single character constant has integer values, known as ASCII values. Formally ASCII is a seven bit code. The valid range is 0 - 127. Many people are used to thinking of it as an 8-bit code because bytes are 8 bits wide and characters are stored in bytes. The range of ASCII values are listed below.
A string constant is a sequence of characters enclosed in double quotes. Hence ‘A’ (is a character constant) is not the same as “A” (is a string constant). The characters comprising the string constant are stored in successive memory locations. Following rules needs to follow, while using a string constants:
A string constant can have one or more characters. For example, “A”, “AA”.
A string constant can have blank space in between two characters or preceded or trailing the first and last characters. For example, “AB CD”, “ AB”, “AB ” are considered as valid string constants.