Program Ape's---C language details (const variable initialization, array size with const variable details, const variable with # define macro, volatile modifier)

Source: Internet
Author: User

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)

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.