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.
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.
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
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.
3.14159 // Valid
314159E-5L // Valid
510E // Invalid: Incomplete exponent
210f // Invalid: No decimal or exponent
.e55 // Invalid: Missing integer or fraction
There are two boolean constants, both of which are standard C++ keywords:

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’).
#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:
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.
#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;
}
hello, runoobhello, runoob
In C++, there are two simple ways to define constants:
Using the #define preprocessor.
#define Preprocessor
The form to define a constant using the #define preprocessor is:
#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:
const Keyword
Using the const prefix to declare a constant of a specified type, in the following form:
const type variable = value;
#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;
}
Generally, when we need to use numbers, we use raw data types such as int, short, long, float, and double.
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:
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.
To utilize these functions, you need to include the math header <cmath>.
#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:
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.
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;
}
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.