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 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. For example, 11.123
is a floating-point constant. C/C++ also allows you to use scientific notation for
floating-point numbers.
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.
For most programs you will write, the compiler defaults are adequate. However,
you can specify precisely the type of numeric constant you want by using a suffix. For
floating-point types, if you follow the number with an F, the number is treated as a
float. If you follow it with an L, the number becomes a long double. For integer types,
the U suffix stands for unsigned and the L for long. Here are some examples:
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 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. For example, 11.123
is a floating-point constant. C/C++ also allows you to use scientific notation for
floating-point numbers.
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.
For most programs you will write, the compiler defaults are adequate. However,
you can specify precisely the type of numeric constant you want by using a suffix. For
floating-point types, if you follow the number with an F, the number is treated as a
float. If you follow it with an L, the number becomes a long double. For integer types,
the U suffix stands for unsigned and the L for long. Here are some examples:
Data type Constant examples
int 1 123 21000 −234
long int 35000L −34L
unsigned int 10000U 987U 40000U
float 123.23F 4.34e−3F
double 123.23 1.0 −0.9876324
long double 1001.2L
No comments:
Post a Comment