"C language C + +" Simple keyword register,const,static,volatile,typedef,define understanding

Source: Internet
Author: User

Register

A variable declared with register is called a register variable, which is stored directly in the register of the machine, if possible, but not for the four-bit compiler when the global When optimizations(global Optimization) is on, it makes a choice whether to put it in its own register, but other symbols related to the Register keyword are The bit compiler is valid.

Const

The const - modified items are protected against accidental changes and can improve the robustness of the program. It can modify the parameters of the function, the return value, or even the definition body of the function. ( especially note that in C ,the variable nature of theconst trim is still a variable; in c + + , it becomes a constant.) )

   1> Parameters

a. For input parameters of non-intrinsic data types, you should change the "value passing" method to " const recursion, the purpose is to improve efficiency. For example, void func (a a)   void func (const a &a)

    b.b. for input parameters of the internal data type, do not change the "value passing" method to " Const reference delivery ". Otherwise, it can not achieve the purpose of improving efficiency, but also reduces the comprehensible function. For example, void func ( int x) should not be changed to void func (const int &x)and should be written as void func (const int x). /c8>.

   2> return value

    A. if the return value of a function with the "pointer passing" method is added Const modifier, the contents of the function return value (that is , the stylus) cannot be modified, and the return value can only be assigned to a const - decorated pointer of the same type. For example:const char * GetString (void); char *str = GetString ();// error const char *STR = GetString ();//< /c8> Correct

   B. if the function return value is in "value passing", the const modifier has no value because the function copies the return value into an external temporary storage unit .

   3> function

in the declaration of a const member function, theconst keyword can only be placed at the end of a function declaration , indicating that the class member does not modify the object

Static

   1> modifier Variables

    A. static global variables, scoped only to files defined by variables, and other files are used extern He was also unable to use the sound . The scope is exactly where the definition begins, ending at the end of the file, and the lines of code preceding the definition cannot be used. If you want to use it, you have to add externto the front.

    B. static local variables, defined within the function body, can only be used in this function, and other functions in the same document cannot be used. Since the static modified variable always exists in memory, even if the function ends, the value of the static variable will not be destroyed and will still be used for the next use.

    2> modifier Functions

The function is static by adding a static to the function . Butthe meaning of "static" Here is not the means of storage, but the scope of the function is confined to this file ( so called intrinsic function ). The advantage of using intrinsic functions is that when you write different functions from someone else, you don't have to worry about the function you're defining and whether it will have the same name as a function in another file.

Volatile

Simply say the role of volatile avoids compiler optimizations. the volatile reminder compiler can change the variables that are defined later, so the compiled program reads the data directly from the variable address each time it needs to store or read the variable. Without the volatile keyword, the compiler might optimize reading and storage, possibly using the value in the register temporarily, and if the variable was updated by another program, there would be an inconsistency.

typedef

the true meaning of a typedef is to give an existing data type (note: A type is not a variable) to take an alias, rather than define a new data type

   1> general variable type definition

   Typedef unsigned char uchar//uchar A; (unsigned char A;)

   2> array type definition

   Typedef int arr[2]//arr A; (int a[2];)

   Typedef int arr[m][n]//arr A; (int arr[m][n];)

   3> pointer type definition

   Typedef int *pointer//pointer p; (int *p;)

   Typedef int *pointer[m]//pointer p; (int *p[m];)

   4> function Declaration

   Typedef int Fun (void)//fun F; (int f (void);)

   Fun *p; (int (*p) (void);)

   5> function Pointers

   Typedef Int (*func) (void)//func p; (int (*p) (void);)

Define

An identifier is allowed in a C or C + + language source program to represent a string, called a macro.

Keyword Usage Summary.

   1> no parameter macro definition

The general form of the definition is : #define identifier string

A. A macro definition is a string represented by a macro name, which in turn replaces the macro name with that string, which is a simple substitution that can contain any character in the string, a constant, or an expression, and the preprocessor does not check it. If there is an error, it can only be found when compiling a source program that has been expanded by the macro.

   b.b. a macro definition is not a description or statement, and a semicolon is not required at the end of the line, and a semicolon is substituted with a semicolon.

   2> with parameter macro definition

The general form of the parameter macro definition is: #define Macro name ( formal parameter list ) string

The parameters in the macro definition are called formal parameters, and the parameters in the macro call are called actual parameters. For a macro with parameters, in the call, not only the macro expansion, but also with the actual parameter to replace the parameter.

   3> Prevent duplicate Definitions

For example

#Ifndef __headerfilexxx__

#Define __headerfilexxx__

...

File contents

...

#EndIf

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C language C + +" Simple keyword register,const,static,volatile,typedef,define understanding

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.