Basic introduction
1) Constants are fixed values and cannot be changed during program execution. These fixed values are also called literal values.
2) Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string literals, as well as enumeration constants.
3) The value of a constant cannot be modified after it is defined.
Frequently used constants
integer constant
Integer constants can be decimal, octal, or hexadecimal constants. Prefix specifies the base: 0X or 0X indicates hexadecimal, 0 indicates octal, and without prefix, it indicates decimal by default. Integer constants can also have a suffix. The suffix is a combination of U and L. U represents unsigned integer and l represents long integer. The suffix can be uppercase or lowercase, and the order of U and l is arbitrary
85: decimal
0213: octal
0x4b: hexadecimal (octal and hexadecimal are explained later)
30: integer
30u: unsigned integer
30l: long integer
30ul: unsigned long integer
Floating-Point Literals
Floating point constants are composed of integer part, decimal point, decimal part and exponential part. You can express floating-point constants in decimal or exponential form.
3.14159; : double constant
314159E-5; : Scientific counting method
3.1f; : float constant
character constants
Character constants are enclosed in single quotes. For example, 'x' can be stored in a variable of type char. A character constant can be an ordinary character (such as' x ') or an escape sequence (such as' \ t')
'X'
'Y'
'A'
'b'
'1'
'\t'
string constant
String literals or constants are enclosed in double quotes' '. A string contains characters similar to character constants: ordinary characters, escape sequences, and common characters. You can use spaces as separators to break a long string constant
"hello, world"
"Beijing"
"hello \
world"
Definition of constants
There are two ways to define constants:
1) Use #define preprocessor.
2) Use const keyword
#define preprocessor
1) The #define preprocessor defines the form of a constant
#define constant name constant value
2) Case demonstration
#include<stdio.h> //Define constant PI #define PI 3.14 void main(){ //printf("Huanhuan!"); //Find the area of a circle PI * r * r double r = 2.3; //Initialize the radius of the circle double area; // Sets the area of the circle area = PI * r * r; printf("The area of the circle is : %.2f",area); }
const keyword
1) You can use const to declare constants of the specified type
const data type constant name = constant value// That is, a statement
2) Case demonstration
#include<stdio.h> const double PI = 3.14; void main(){ //Define the radius of the circle as 3.4 double r = 3.4; //Define area area double area; //Assign area area = PI * r * r; printf("The area of the circle is : %.2f",area); }
Note: once defined, constants cannot be changed.
The difference between const and #define
1) const defines a constant with a type, and define without a type
2) const works when compiling and running, while define works in the preprocessing stage of compilation
3) define is a simple replacement without type checking. Simple string replacement will lead to boundary effect [case demonstration]
#include<stdio.h> #define A 1 #define B (A+3) #define C A/B*3 void main(){ //The analysis process / /#define is a simple replacement!!! //C is actually A/A+3*3 = 1/1 + 3 * 3 = 1 + 9 = 10 //C is actually A/(A+3)*3 = 1/(1+3) * 3 = 1/4 *3 =? printf("\nc=%.2f", C);// Q c =? double d1 = 1.0/4*3 ; // 0.25 * 3 = 0.75 printf("\nc=%.2f", C);// Q c =? printf("\nd1=%.2f", d1); }
4) const constant can be debugged, but define cannot be debugged. It is mainly replaced in the precompile stage, and it is not available during debugging
5) const cannot be redefined, and two identical symbols cannot be defined. define cancels the definition of a symbol through undef, and then redefines [case]
//const cannot be redefined. You cannot define two symbols that are the same. Define cancels the definition of a symbol through undef and redefines it const double PI=3.14; //const double PI=3.145; #define PI2 3.14 #undef PI2 / / undefine PI2 #define PI2 3.145
6) Define can be used with #ifdef, #ifndef, #endif to make the code more flexible. For example, we can start or close debugging information through #define. [case]
//The second case illustrates the difference between const and define #include <stdio.h> //#define DEBUG void main() { #ifdef DEBUG / / if defined DEBUF printf("ok, debug information"); #endif #ifndef DEBUG / / if not defined DEBUF printf("hello, Additional information"); #endif