Dev C++ Const

admin

A little-known feature of C++ is that the cmath library actually provides many mathematical constants that you can make use of in your quantitative finance programs.

Constipation

Const constant-name = constant-value; Example to define constant using const. Const float PI = 3.14159f; float const e = 2.71828f; Defining constant using #define. #define is a pre-processor directive used to define constants and macros. It defines compile time constant and guarantees 100% constantness. Win32 - 'Win32 is a C library used to build windows applications. Win32 is a free alternative to MFC. It has the added advantage of being able to run on a wide range of free compilers, including Visual Studio Community, and the MinGW compiler provided with CodeBlocks and Dev-C.'

To include the mathematical constants, you need to use a #define macro called _USE_MATH_DEFINES and add it before importing the cmath library:

There are quite a few constants on offer. See if you can spot the ones that will be useful in quantitative finance:

Mathematical ExpressionC++ SymbolDecimal Representation
piM_PI3.14159265358979323846
pi/2M_PI_21.57079632679489661923
pi/4M_PI_40.785398163397448309616
1/piM_1_PI0.318309886183790671538
2/piM_2_PI0.636619772367581343076
2/sqrt(pi)M_2_SQRTPI1.12837916709551257390
sqrt(2)M_SQRT21.41421356237309504880
1/sqrt(2)M_SQRT1_20.707106781186547524401
eM_E2.71828182845904523536
log_2(e)M_LOG2E1.44269504088896340736
log_10(e)M_LOG10E0.434294481903251827651
log_e(2)M_LN20.693147180559945309417
log_e(10)M_LN102.30258509299404568402

Note that it is not best practice within C++ to use #defines for mathematical constants! Instead, as an example, you should use const double pi = 3.14159265358979323846;. The #defines are a legacy feature of C.

  • C Programming Tutorial
  • C Programming useful Resources
  • Selected Reading

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.

Constants are treated just like regular variables except that their values cannot be modified after their definition.

Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Here are some examples of integer literals −

Following are other examples of various types of integer literals −

Floating-point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

While representing decimal form, you must include the decimal point, the exponent, or both; and while representing exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Here are some examples of floating-point literals −

Character Constants

Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type.

Dev C++ Constant

Dev c++ constipation

A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., 't'), or a universal character (e.g., 'u02C0').

There are certain characters in C that represent special meaning when preceded by a backslash for example, newline (n) or tab (t).

  • Here, you have a list of such escape sequence codes −
      Escape sequenceMeaning
      character
      ' ' character
      '' character
      ?? character
      aAlert or bell
      bBackspace
      fForm feed
      nNewline
      rCarriage return
      tHorizontal tab
      vVertical tab
      oooOctal number of one to three digits
      xhh . . .Hexadecimal number of one or more digits

Following is the example to show a few escape sequence characters −

When the above code is compiled and executed, it produces the following result −

String Literals

String literals or constants are enclosed in double quotes '. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

You can break a long line into multiple lines using string literals and separating them using white spaces.

Here are some examples of string literals. All the three forms are identical strings.

Defining Constants

There are two simple ways in C to define constants −

  • Using #define preprocessor.

  • Using const keyword.

Dev C++ Install For Windows10

The #define Preprocessor

Given below is the form to use #define preprocessor to define a constant −

The following example explains it in detail −

When the above code is compiled and executed, it produces the following result −

The const Keyword

You can use const prefix to declare constants with a specific type as follows − Knife sketch.

The following example explains it in detail −

When the above code is compiled and executed, it produces the following result −

Note that it is a good programming practice to define constants in CAPITALS.