Program --- C language details 28 (const variable initialization, array size with const variable details, const variable and # define macro, volatile modification)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.