C++ Constants and C++ Numbers

1. C++ Constants
A constant is a fixed value that does not change during program execution. It can be of any basic data type and can be classified into integer numbers, floating-point numbers, characters, strings, and boolean values.
01
Integers
Integer constants can be in decimal, octal, or hexadecimal form.
The prefix specifies the base: 0x or 0X indicates hexadecimal, 0 indicates octal, and no prefix indicates decimal by default.
Integer constants can also have a suffix, which is a combination of U and L, where U indicates an unsigned integer and L indicates a long integer.
The suffix can be uppercase or lowercase, and the order of U and L can be arbitrary.
Example:
212         // Valid
215u        // Valid
0xFeeL      // Valid
078         // Invalid: 8 is not an octal digit
032UU       // Invalid: Cannot repeat suffix
85         // Decimal
0213       // Octal
0x4b       // Hexadecimal
30         // Integer
30u        // Unsigned integer
30l        // Long integer
30ul       // Unsigned long integer
02
Floating Point
Floating-point constants consist of an integer part, a decimal point, a fractional part, and an exponent part.
Floating-point constants can be expressed in either decimal or exponential form.
When using decimal form, the integer part and the fractional part must be included, or both must be present. When using exponential form, the decimal point and exponent must be included, or both must be present. The signed exponent is introduced using e or E.
Example:
3.14159       // Valid
314159E-5L    // Valid
510E          // Invalid: Incomplete exponent
210f          // Invalid: No decimal or exponent
.e55          // Invalid: Missing integer or fraction
03
Boolean
There are two boolean constants, both of which are standard C++ keywords:
True represents true.
False represents false.

C++ Constants and C++ Numbers

04
Character
Character constants are enclosed in single quotes.
If a constant starts with L (only when uppercase), it indicates a wide character constant (e.g., L’x’), which must be stored in a wchar_t type variable. Otherwise, it is a narrow character constant (e.g., ‘x’), which can be stored in a char type variable.
Character constants can be a regular character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).
In C++, certain specific characters have special meanings when preceded by a backslash, used to represent newline characters (‘\n’) or tab characters (‘\t’).
Escape Sequence Codes:
C++ Constants and C++ Numbers
Example:
#include <iostream>
using namespace std;
int main(){
    cout << "Hello\tWorld\n\n";
    return 0;
}
The above code, when compiled and executed, will produce the following result:
Hello   World
05
String
String literals or constants are enclosed in double quotes “”.
A string contains characters similar to character constants: regular characters, escape sequences, and universal characters.
A backslash \ can be used as a separator to split a long string constant into multiple lines.
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
    string greeting = "hello, runoob";
    cout << greeting;
    cout << "\n";     // Newline character
    string greeting2 = "hello, \
                       runoob";
    cout << greeting2;
    return 0;
}
Result:
hello, runoobhello, runoob
06
Definition
In C++, there are two simple ways to define constants:
Using the #define preprocessor.
Using the const keyword.

#define Preprocessor

The form to define a constant using the #define preprocessor is:
#define identifier value
Example:
#include <iostream>
using namespace std;
#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'
int main(){
int area;  
   area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}

Result:

50

const Keyword

Using the const prefix to declare a constant of a specified type, in the following form:
const type variable = value;
Example:
#include <iostream>
using namespace std;
int main(){
const int  LENGTH = 10;
const int  WIDTH  = 5;
const char NEWLINE = '\n';
int area;  
   area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
Result:
50
2. C++ Numbers
Generally, when we need to use numbers, we use raw data types such as int, short, long, float, and double.
01
Definition
Direct comprehensive example
#include <iostream>
using namespace std;
int main (){
   // Number definitions   short  s;
   int    i;
   long   l;
   float  f;
   double d;
   // Number assignments   s = 10;         
i = 1000;       
l = 1000000;    
f = 230.47;     
d = 30949.374;
   // Number output   cout << "short  s :" << s << endl;   
cout << "int    i :" << i << endl;   
cout << "long   l :" << l << endl;   
cout << "float  f :" << f << endl;   
cout << "double d :" << d << endl;
   return 0;
}
The result produced after compilation and execution:
C++ Constants and C++ Numbers
02
Mathematical Operations
In C++, in addition to creating various functions, there are also many useful functions available for use.
These functions are written in the standard C and C++ libraries, called built-in functions, which can be referenced in programs.
C++ has a rich set of built-in mathematical functions that can perform operations on various numbers.
C++ Constants and C++ Numbers
To utilize these functions, you need to include the math header <cmath>.
Example:
#include <iostream>
#include <cmath>
using namespace std;
int main (){
   // Number definitions   short  s = 10;
   int    i = -1000;
   long   l = 100000;
   float  f = 230.47;
   double d = 200.374;
   // Mathematical operations   cout << "sin(d) :" << sin(d) << endl;   
cout << "abs(i)  :" << abs(i) << endl;   
cout << "floor(d) :" << floor(d) << endl;   
cout << "sqrt(f) :" << sqrt(f) << endl;   
cout << "pow( d, 2) :" << pow(d, 2) << endl;
   return 0;
}
The result produced after compiling and executing:
C++ Constants and C++ Numbers
03
Random Numbers
During programming, there are many business scenarios that require generating random numbers.
There are two related functions for random number generation. One is rand(), which only returns a pseudo-random number. You must call srand() before generating random numbers.
Example:
Use the time() function to get the system time in seconds and call the rand() function to generate random numbers.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main (){
int i,j;
// Set seed   srand( (unsigned)time( NULL ) );
/* Generate 10 random numbers */
for( i = 0; i < 10; i++ )   {
   // Generate actual random number      j= rand();
cout <<"Random Number:" << j << endl;   }
return 0;
}
——- END ——-
Disclaimer

This article’s images and text are partially sourced from the internet.

If there are any copyright issues, please contact us in a timely manner.

Leave a Comment