The const analysis of the 3rd course after evolution

Source: Internet
Author: User

1. Const in the C language

(1) The const-modified variable is read-only , making the variable read-only, but essentially a variable . So not a true constant, it just tells the compiler that the variable cannot appear to the left of the assignment symbol.

(2) const-Modified local variable allocates space on stack , global variable allocates space in read- only storage

(3) const only useful at compile time , useless at run time

Const in the "programming experiment" C + +

#include <stdio.h>intMain () {Const intc =0;//The C language allocates memory for variable C    int* p = (int*) &c;//& only allocates memory for C C + +printf ("begin...\n"); *p =5;//the value in memory has already been changed to 5.printf ("C =%d\n", c);//the C language outputs 5 of the memory. //In C + +, values are taken from the symbol table (not memory)//so for 0.printf ("end...\n"); return 0;}

2. Const in C + +

(1) C + + gives precedence to const on the basis of C, and puts constants in the symbol table when a const declaration is encountered.

(2) If a constant is found during compilation, it is replaced directly by the value in the symbol table .

(3) If you find that the extern or & operator is used for const constants during compilation, the corresponding constants are allocated storage space . Note that although the C + + compiler may allocate space for const constants, it does not use the values in its storage space.

3. Comparing the const in C/

C language

C++

Essence

Read-only variables

Constant

Allocating memory

Will be assigned

When you use the & operator to assign addresses to const constants

Memory is allocated when Const constants are global and need to be used in other files

4. The difference between a const and a macro in C + +

Const in C + +

Macro

Defined

const int c = 5;

#define C 5

Processing mode

Handled by the compiler , the compiler does type checking and scope checking

Processed by the preprocessor , just simple text substitution

"Programming Experiment" Const and macro

#include <stdio.h>voidf () {//macros are processed by precompilation, and the macros behind them work    #defineA 3Const intb =4;//scope is limited to the F function}voidg () {printf ("A =%d\n", a);//valid, as long as the macro definition can be used//printf ("b =%d\n", b);//the scope of illegal b is limited to the F function}intMain () {Const intA =1; Const intB =2; intArray[a + B] = {0};//C + + is legal because it thinks A and B are constants. //the const nature of C is still a variable, and the size of the array can only be constant .    inti =0;  for(i=0;i< (A + B); i++) {printf ("array[%d] =%d\n", I, array[i]);    } f ();    g (); return 0;}

5. Summary

(1) Unlike the C language, const in C + + is not a read-only variable

(2) const in C + + is a true constant

(3) C + + compiler may allocate space for const constants

(4) C + + is fully compatible with the syntax characteristics of const constants in C language

The const analysis of the 3rd course after evolution

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.