What is Constants and Its types
Constants
Constants refer to fixed values that the program may not 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.
Character constants are enclosed between single quotes. For example ‘a’ and ‘%’ are both character constants. Both C and C++ define wide characters (used mostly in non-English language environments), which are 16 bits long. To specify a wide character constant, precede the character with an L. For example,
wchar_t wc;
wc = L’A';
Here, wc is assigned the wide-character constant equivalent of A. The type of wide characters is wchar_t . In C, this type is defined in a header file and is not a built-in type. In C++, wchar_t is built in.
Integer constants are specified as numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants require the decimal point followed by the number’s fractional component.
There are two floating-point types:
Float and Double. There are also several variations of the basic types that you can generate using the type modifiers. By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. Therefore, assuming 16-bit integers, 10 is int by default, but 103,000 is a long.
Even though the value 10 could fit into a character type, the compiler will not cross type boundaries. The only exception to the smallest type rule are floating-point constants, which are assumed to be doubles.
Hexadecimal and Octal Constants
It is sometimes easier to use a number system based on 8 or 16 rather than 10 (our standard decimal system). The number system based on 8 is called octal and uses the digits 0 through 7. In octal, the number 10 is the same as 8 in decimal. The base 16 number system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F, which stand for 10, 11, 12, 13, 14, and 15, respectively. For example, the hexadecimal number 10 is 16 in decimal. Because these two number systems are used frequently, C/C++ allows you to specify integer constants in hexadecimal or octal instead of decimal. 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 = 0×80; /* 128 in decimal */
int oct = 012; /* 10 in decimal */
String Constants
C/C++ supports one other type of constant: the string. A string is a set of characters enclosed in double quotes. For example, “this is a test” is a string. You have seen examples of strings in some of the printf() statements in the sample programs. Although C allows you to define string constants, it does not formally have a string data type. (C++ does define a string class, however).
A single character constant is enclosed in single quotes, as in ‘a’. However, “a” is a string containing only one letter.
Backslash Character Constants
Enclosing character constants in single quotes works for most printing characters. A few, however, such as the carriage return, are impossible to enter into a string from the keyboard. For this reason, C/C++ include the special backslash character constants, so that you may easily enter these special characters as constants.
These are also referred to as escape sequences. You should use the backslash codes instead of their ASCII equivalents to help ensure portability. For example, the following program outputs a new line and a tab and then prints
the string This is a test.
#include
int main(void)
{
printf(”\n\tThis is a test.”);
return 0;
}
Backlash Codes
Backlash codes
Code Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\0 Null
\\ Backslash
\v Vertical tab
\a Alert
\? Question mark
\N Octal constant (where N is an octal constant)
\xN Hexadecimal constant (where N is a hexadecimal
constant)
Popularity: 1% [?]