Program --- C language details 28 (const variable initialization, array size with const variable details, const variable and # define macro, volatile modification ),
Main Content: const variable initialization, array size using const variable details, const variable and # define macro, volatile Modification
1. the const variable must be assigned a value during initialization.
2. the const variable can be an array size element in C ++ and cannot be used in C because it is a variable.
Iii. Difference Between const and # define: Memory Allocation
Iv. Modify volatile variables: variables that are easy to be modified by the operating system, hardware, and multithreading.
# Include <stdio. h> int main () {/* Test 1 */const int B; // if it is not initialized, an error is returned. // B = 2; /* Test 2 * an error is reported when the array size is defined in the C language. It can be seen that the num modified by const in the C language is a variable, instead of a constant *, C ++ can use */const int num = 2 for compiling; // int a [num] = {3, 4 }; /* Test 3 */# define M 4 // macro constant const int N = 5; // N is not put into memory at this time and saved in the symbol table int I = N; // allocate memory for N at this time, and no int I = M will be allocated in the future; // macro replacement and memory allocation for the sake of pre-compilation (M has no type, how to allocate memory) int j = N; // no memory allocation int J = M; // macro replacement again, memory allocation once/* Test 4 during the following test, vc ++ 6.0 is not optimized in general Debug mode, but both Debug and Release versions can be generated for testing */int test = 10; int test_1 = test; int test_2 = test; // when the value is assigned here, the compiler will not generate the Assembly to re-read the test value from the memory (test is not used as the left value between the two values, otherwise it will not be optimized) volatile int t; int t_1 = t; int t_2 = t; // when the value is assigned here, t reads const volatile int temp from the memory again; // temp read-only, which can represent read-only registers}Output:
No output in this example