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
Int main () {/* Test 1 */const int B; // If Initialization is not performed, an error is returned. // B = 2;/* Test 2 * If the array size is defined in the C language, an error is returned, it can be seen that the num modified by const in C language is a variable, not a constant *, but in C ++, it can be compiled using */const int num = 2; // 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 is saved in the symbol table int I = N; // memory is allocated for N at this time, and int I = M is not allocated in the future; // perform macro replacement for pre-compilation and allocate memory (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, however, you can generate both Debug and Release versions for testing */int test = 10; int test_1 = test; int test_2 = test; // when values are assigned here, the compiler will not generate an 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