Main content: Const variable initialization, array size with const variable details, const variable and # define macro, volatile modifier
A const variable must be assigned when initialized
Second, the const variable in C + + can do array size elements, not in C, because it is a variable
Three, const and # define differences: Memory allocation
Four, volatile modified some variables: easy to manipulate the system, hardware, multi-threaded modified variables
#include <stdio.h>int main () {/* Test 1 */const int B; Uninitialized error//b = 2;/* Test 2 *c language The following definition array size will be an error, you can see the C language const decorated num is a variable, not a constant * and C + + is compiled through can be used */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, save in the symbol table int i = N; The memory is allocated for n at this time, and the int I = M is no longer assigned; Macro substitution for precompiled purposes, allocating memory (m no type, how to allocate memory) int j = N; No memory allocations int J = M; Macro replace again, one time to allocate memory/ * Test 4 When doing the following test, vc++6.0 General debug mode is not optimized, but can generate both debug and release version to do test */int testing = 10;int test_1 = test;int test_2 = test; When assigning a value here, the compiler does not generate the assembly to read the test value again from memory (between two assignments, test is not left, otherwise not optimized) volatile int t;int t_1 = t;int t_2 = t; When assigning a value here, T re-reads the const volatile int temp from memory; Temp read-only, can represent read-only registers}
Output:
This example has no output
Program Ape's---C language details (const variable initialization, array size with const variable details, const variable with # define macro, volatile modifier)