Const define static extern keyword detailed

Source: Internet
Author: User

Const

The Const keyword does not change a variable to a constant, and a const in front of the symbol indicates that the symbol cannot be assigned, that is, his value is read-only for the symbol, but does not mean that the value cannot be changed in any other way. The following example will be a good understanding,

int i = 5;
const int *a = &i;
*a = 8; Error, read-only can't assign value
i = 10; Ok

Usually we see a lot of the const usage has the following, it seems to be summed up as the "nearest principle": That is , const closest to its right which variable, it means that the variable is a constant. "

const int *p1 /* P1 the value of the int variable is immutable, constant, but can change the value of the P1 pointer */
int * const p2; /* p2 pointer is constant, that is, the value of P2 is immutable, but you can change the value of P2 to the object */
const int * const p3/* p3 pointer is constant, and the value of the Int object that P3 points to is also constant */

< Span style= "color: #ff9900;" >       //const
    
//    How do I tell what part of the const modifier is?
//    int const *P = &a;
//    1. Remove the base data type (such as int)
//    const *P = &a;
    2. After starting from Const, this is the amount that cannot be modified
//    *p
//    3. If you don't know what the *p is referring to, write the equation
//     *p =?
//    At this point, the const modifier is that the content pointed to by the pointer p cannot be modified
//     int a = ten;
    const int * Const P = &a;//*p =? p =?
//    int * Const P = &a; //  p =?
//    int Const *P = &a;  //  *p =?
//    const int *p = &a;  //  *p =?

Role

1) Enhanced program security (anti-modification)

2) such as strcpy, strlen, memcpy, etc.

Define

Macro Definition Format
#define Macro-Name expressions
Macro names are generally capitalized

The role of macros
1. Define some of the more commonly used quantities
2. Define some of the more common operations
How does a macro work?
The macros are replaced during precompilation

1) in C language, the main function of the macro is to do the replacement

2) The use of macros can make the program more easy to modify

3) The macro is divided into:

3.1) No parameter macros

3.1.1) #define MAX 99

3.1.2) For example:

3.1.3) int I=max;

3.1.4) constant equals

3.1.5) int i=99;

3.1.6) Max is called the macro name. Max can be used wherever 99 is used

3.2) with the parameter macro

3.2.1) #define Kmaxvalue (A, B) (a>b?) A:B)

3.2.2) using:

3.2.3) int a=kmaxvalue (3,5);

3.2.4) a=?

4) Replace at precompilation

5) Macro name naming rules:

Pure capitalization or

K + Hump

6) such as MAX or Kmax

7) Macros are not variables

Static


1) static for modifying variables

2) Statically modified variables are stored in the static area

3) If no value is assigned, the default is 0

4) initialized before main, can only be initialized once, but can be assigned multiple times

5) static int a=10;//a stored in the global zone

6) static int b[10]={0};//The entire array is stored in the global zone

7) Static int*p=null;//pointer is stored in the global zone

8) ... Global zone variables are released at the end of the program

Const define static extern keyword detailed

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.