A constant is a value that does not change while the
program in running. There
are various categories of constants you will use: those that are mathematically defined (such as
numbers) and those that are defined by, and are part of, the C++/CLI language. To make their management easier, these constant values have
been categorized and defined in particular libraries.
The algebraic numbers you use all the
time are constants because they never change. Examples of constant
numbers are 12, 0, 1505, or 88146. Therefore, any number you can think
of is a constant.
Every letter of the alphabet is a constant and is
always the same. Examples of constant letters are d, n, c.
Some characters on your keyboard represent symbols
that are neither letters nor digits. These are constants too. Examples
are #, &, |, !.
There are two main techniques you use to display a
constant value in C++/CLI. To simply display it using Write
or WriteLine, you can type its value in the parentheses. Here is
an example:
using namespace System;
int main()
{
Console::WriteLine(2850);
return 0;
}
This would produce:
2850
Press any key to continue
You can also define it first using an appropriate
name, and then using that name to display the constant. Here is an
example:
using namespace System;
int main()
{
typedef unsigned int Positive;
Positive Number = 1450;
Console::WriteLine(Number);
Console::WriteLine();
return 0;
}
You can create your own constant that represents
anything you want. The safest technique of using a constant is to give
it a name. This allows you to manage it from one standpoint. For
example, if you plan to use a number such as 3.14,
you can simply use the constant 3.14. Imagine you want to use 3.14 in
various sections of the program. If you decide to change the number from
3.14 to 3.14159 or another value, you would have to find every mention
of 3.14; this can be cumbersome and considered bad programming. The
alternative is to declare a variable and assign it the desired value.
That new and defined value is called a constant.
To create a constant, you use the const
keyword. The simplest way you can do this is to type the const
keyword followed by a data type, followed by a name, followed by =, followed by the desired
value, and terminated by a semi colon. Here is an example:
const int NumberOfDoors = 4;
Here is an example:
using namespace System;
int main()
{
const int NumberOfDoors = 16;
Console::Write("Number of doors = ");
Console::WriteLine(NumberOfDoors);
return 0;
}
This would produce:
Number of doors = 16
Press any key to continue
If you already know (and have included) a constant,
you can initialize your new constant with it. Here is an example:
using namespace System;
int main()
{
const double Value = 125.558;
const unsigned short int Alley = 88;
const double Root = Value;
return 0;
}
Because constants are highly used in mathematics and other
scenarios, both the C++ language and the .NET Framework are
equipped with various constants. Although you can create your own constants
anytime, you should be aware of these constants and usually use them instead of
creating new ones, unless yours are more reliable.
Some of the constants that are part of the C++ language are
defined in the limits.h library. Other constants are defined in various
other files such as float.h. For example, the minimum value of the short integer is SHRT_MIN;
in the same way, the maximum short integer is identified by SHRT_MAX.
You can use either of these constants as follows:
#include <limits.h>
using namespace System;
int main()
{
Console::Write("The maximum of a short value is ");
Console::WriteLine(SHRT_MAX);
return 0;
}
The constant values defined in limits.h are
Name |
Value |
Description |
SCHAR_MAX |
127 |
Maximum of a signed char |
SCHAR_MIN |
128 |
Minimum of a signed char |
UCHAR_MAX |
255 (0xff) |
Maximum of an unsigned char |
CHAR_BIT |
8 |
Number of bits in a char |
USHRT_MAX |
65535 (0xffff) |
Maximum unsigned short value |
SHRT_MAX |
32767 |
Maximum (signed) short value |
SHRT_MIN |
32768 |
Minimum (signed) short value |
UINT_MAX |
4294967295 (0xffffffff) |
Maximum unsigned int value |
ULONG_MAX |
4294967295 (0xffffffff) |
Maximum unsigned long value |
INT_MAX |
2147483647 |
Maximum (signed) int value |
INT_MIN |
21474836471 |
Minimum (signed) int value |
LONG_MAX |
2147483647 |
Maximum (signed) long value |
LONG_MIN |
21474836471 |
Minimum (signed) long value |
CHAR_MAX |
127 (255 if /J option used) |
Maximum char value |
CHAR_MIN |
128 (0 if /J option used) |
Minimum char value |
MB_LEN_MAX |
2 |
Maximum number of bytes in multibyte char |
_I64_MAX |
9223372036854775807 |
Maximum (signed) __int64 value |
_I64_MIN |
-9223372036854775807-1 |
Minimum (signed) __int64 value |
_UI64_MAX |
0xffffffffffffffff |
Maximum (unsigned) __int64 value |
The constant values defined in float.h are:
Name |
Value |
Description |
DBL_DIG |
15 |
# of decimal digits of precision |
DBL_EPSILON |
2.2204460492503131e-016 |
Smallest such that 1.0+DBL_EPSILON !=1.0 |
DBL_MANT_DIG |
53 |
# of bits in mantissa |
DBL_MAX |
1.7976931348623158e+308 |
Maximum value |
DBL_MAX_10_EXP |
308 |
Maximum decimal exponent |
DBL_MAX_EXP |
1024 |
Maximum binary exponent |
DBL_MIN |
2.2250738585072014e-308 |
Minimum positive value |
DBL_MIN_10_EXP |
(-307) |
Minimum decimal exponent |
DBL_MIN_EXP |
(1021) |
Minimum binary exponent |
_DBL_RADIX |
2 |
Exponent radix |
_DBL_ROUNDS |
1 |
Addition rounding: near |
FLT_DIG |
6 |
Number of decimal digits of precision |
FLT_EPSILON |
1.192092896e-07F |
Smallest such that 1.0+FLT_EPSILON !=1.0 |
FLT_MANT_DIG |
24 |
Number of bits in mantissa |
FLT_MAX |
3.402823466e+38F |
Maximum value |
FLT_MAX_10_EXP |
38 |
Maximum decimal exponent |
FLT_MAX_EXP |
128 |
Maximum binary exponent |
FLT_MIN |
1.175494351e-38F |
Minimum positive value |
FLT_MIN_10_EXP |
(37) |
Minimum decimal exponent |
FLT_MIN_EXP |
(125) |
Minimum binary exponent |
FLT_RADIX |
2 |
Exponent radix |
FLT_ROUNDS |
1 |
Addition rounding: near |
A macro is an action you want the compiler to perform for
your program. The C language provides a preprocessor used to
create macros. The particularity of macros, or most macros, is that the
compiler counts on you to know what you are doing. Many of such macros
are created using the #define preprocessor. Based on this concept of macros, you can create a
constant.
The formula of creating a constant using the #define
is:
#define ConstantName ConstantValue
The # symbol and the define word
are required; they inform the compiler that the name following them represents a constant.
The ConstantName represents a valid name for
the desired constant; the name follows the same rules we learned for
defining names. To distinguish the constant from other names of
variables, it is sometimes a good idea to write it in uppercase,
although it is perfectly normal to have it in lowercase or any case
combination you desire.
The ConstantValue can be a character, an
integer, a floating-point value, or an expression. If the constant value
is an integer or a floating-point value, you can type it. If the value
is a character, include it between single-quotes. If the constant value
is a string, include it between double-quotes. The definition of the
constant does not end with a semi-colon.
Examples of declaring constants are:
#define AGE 12 // AGE represents the constant integer 12
#define ANSWER y
#define MAXSTUDENTS 35
#define And "&&"
#define PI 3.14159 // PI represents 3.14159
#define Country = New Zealand;
|
As you can see, there is no precision as to what
type of constant you are creating when using #define. The truth
is that #define doesn't create a constant. It is simply telling
the compiler that the word after #define will be used to
represent whatever follows that ConstantName. This means that, in
reality, what follows ConstantName can be anything; it can even
be an expression. here is an expression:
using namespace System;
int main()
{
#define Welcome Console::WriteLine("Welcome to the wonderful world of C++/CLI!")
#define Print Console::WriteLine()
Welcome;
Print;
return 0;
}
This would produce:
Welcome to the wonderful world of C++/CLI!
Press any key to continue
As you can see, there is no precision as to what type of
constant you are creating when using #define. In reality, #define
doesn't create a constant. It is simply telling the compiler that the word after
#define will be used to represent whatever follows that ConstantName.
This means that what follows ConstantName can be anything;
it can even be an expression. here is an expression:
using namespace System;
int main()
{
#define Add255To1250 255 + 1250
Console::Write("Addition: ");
Console::WriteLine(Add255To1250);
return 0;
}
In reality, the #define technique was implemented in
the C language and C++, as a child of C, simply inherited it from its parent.
The #define routine is still supported and you will encounter it in many
documents. In fact, you are not necessarily discouraged from using it but the
golden rule is to know what you are doing.
|