C Language header file, static and const keywords

Source: Internet
Author: User

Objective

In recent months to do a C language code refactoring project, the process also let me on the books learned in some of the things to supplement and consolidate, in this blog summary record, comb under the fragmented knowledge points and experience also deepen the impression, writing is for better thinking. Usually also use the impression notes and so on their own writing notes, today organized into a blog, so that they later review.

Body

  • C Language Header file: It is understood as C-language modular programming to provide an interface profile to the outside. It typically includes variable declarations, function declarations, data type definitions, and macro definitions for external functions or files. Example:
  • /******************************************************************************* Header File test001.h**************** ***************************************************************/#ifndef test001_h_included//Prevent header file re-introduction#definetest001_h_included/** In the source file test001.c: #define TEST001_GLOBAL_VALUE_DEFINE* Set macro ifdef judgment, If this header file is introduced in a file other than test001.c, the following variable declaration and function declaration are added with the extern modifier * declared as an external function and a global variable*/#ifdef Test001_global_value_define#defineTrns_global#defineTrns_global_val (v) = (v)#else#defineTrns_global extern#defineTrns_global_val (v)#endif//function DeclarationTrns_globalvoidInittest (void); Trns_globalvoidEventstart (int*Point ); //Variable DeclarationTrns_globalDoubleF_initgetreq; Trns_globalDoublef_errconfirm;#defineBlm_send_buff_size 12#defineBlm_recv_buff_size 128#undefTrns_global#undefTrns_global_val#endif

  • Static keyword:
    • Global static variable: Declare a static variable outside of any function body so that it is valid only in this file, and its visible range is from the location where it is defined to the end of the file and not visible to other modules.
    • Local static variables: Declare a static variable in the body of the function, its visible scope is this function, only the first time this function is called, the static variable is defined, its life cycle continues until the end of the entire program, subsequent calls to the function, its value remains the last call after the value, will not be repeated definition.
    • static function: A function declared as static, indicating that it is only available within the module, not as an external interface, can be understood as private. Other files do not see, all have the same name function is not related, do not affect each other.
  • The CONST keyword:
    • modifier variable:
       //  two forms, defining that value is an immutable int variable  int  const  Value = 10  ;  const  int  Value = 10   //  external links, can be declared only, not initialized  extern  const  int  value = 20 ; 
    • Modifier pointers:
      // two types, the pointer itself is constant, immutable Char Const PValue;   Const (Char*// Two forms, the object pointed to by the pointer is constant constChar * pValue;   Char Const *// Both are constants constcharconst
    • Modifier function Parameters:
      //parameter is immutable in the function body (meaningless, formal parameter, passed argument is a copy)voidfunctionConst intNum); //the content that the parameter pointer points to is immutable within the function bodyvoidfunctionConst Char*Num);//The parameter pointer is a constant (meaningless, the pointer is a formal parameter)voidfunctionChar*ConstpValue);//The reference parameter is immutable within the function bodyvoidfunctionConst int& Num);
    • Modifier function return Value:
      // represents a constant for a function return value and can only be assigned to a const-decorated pointer of the same type Const Char* function (); Const char* str = function (); Char* str1 = function (); // Error
    • Function and Advantage: Protect the modified members, prevent accidental modification, enhance the robustness of the program. Const is more secure than a # define precompiled directive, which is simply a substitution without type checking. The common const constant in the compiler allocates storage space, saves it in the symbol table, determines its value and does not change during compilation, and eliminates the memory stored in read-write operations to improve efficiency.

    • The file contains:
      • It can be understood that file A contains file B, that is, a copies the contents of file B to expand at the point of introduction, equivalent to merging two files.
      • So simple file inclusions, especially. c files, can easily produce duplicate definitions.
      • The. h file can also define variables, but it is not recommended to do so, on the one hand, to introduce a duplicate definition of the header file, the header file also adds a macro definition header to prevent repeated introduction.

C language header file, static and const keywords

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.