Ad Code

Responsive Advertisement

C++ Constants - Learn CPP

C++ Constants - Learn CPP, Constants: They are fixed values whose value does not change throughout the program execution period, that is, they become readable only, and are divided into numerical constants and symbolic constants.


C++ Constants - Learn CPP


Types of constants in C++


1- Numerical constants:


It includes:

  • Integer: Like the numbers 1,6- , 7.700+.
  • True scalar constant (Float): eg 17.5, +655.3+ , 636.9-.


2- Symbolic Constants


They are language codes, and they consist of letters and numbers, and they are between two quotation marks, such as: (“Tamara” - “2001” - “T” - “2 + 6 - “$”).


Constants can be a number, a letter, a symbol, or a text, and their value cannot be changed. Sometimes we need to use values ​​that may be known in advance before executing the program, and this value cannot change within the program, such as pi() which has an approximate value (3.14159), these constant values ​​whether they are of a pre-known value, or any A value can be based on a variable, all of which can be declared anywhere in the body of the program in more than one way.


Note: Identifiers that are defined as constants cannot change their values ​​during program execution in any way.


How to declare a constant in C++


The first method is using (Const)


It precedes data types to define one or more variables as constant, where the sentence begins with the word (Const), then mentions the type of the variable and then its name so that they are separated by a space and then a sign - and then the value of the constant, and the sentence ends with a semicolon.


An example is Const Float Pi=3.14.


The second method using #Define:


It defines symbols as constants, and although this structure is not commonly used in C++, it can be used to define arithmetic or symbolic variables at the beginning of the program, and compensate for their arithmetic or symbolic value wherever these names are mentioned in the program and the alphabet is used Usually capitalized to define the names of these variables, for example:


  • #define EOE -1
  • #define TRUE 1 


Public format


  • #define Constant value:
  • Step 1: #define MAX 97;
  • Step 2: main()
  • Step 3: {
  • Step 4: court<<Max
  • Step 5:}
  • The output will be: 100


Note: This structure is common in C, and everything in C can be used in C++, and vice versa.