The function of OC Learning 2--c language characteristics

Source: Internet
Author: User
Tags function definition

1, OC is on the basis of the C language to expand, in OC directly in C language coding can also be compiled. Therefore, the syntax format for the function definition is as follows:

function return value type  function name (formal parameter list) {   // function         with 0 or more executable statements }

2, the function of the transfer mechanism: value delivery, address delivery .

Value passing: a copy (replica) of the actual parameter value is passed into the function, and the parameter itself is not affected by the change in its copy within the function.

Address delivery: A copy of the address of the actual parameter is passed into the function, and the value of the corresponding position in the function is changed to affect the value of the actual parameter.

3. Internal functions and external functions:

intrinsic function: The function is defined with a static modifier , which can only be called by other functions in the current source file, which is called an intrinsic function. Internal functions have better cohesion, which guarantees that the function can only be called in the source file, which avoids the problem of conflicting names of functions in multiple source files .

External functions: Use the extern modifier when defining a function, or do not use any modifier modifiers, which can be called by functions in any file, which is called an external function. Generally used in the definition of library functions .

4. Local variables and global variables:

Local variables: They are divided into function local variables and code block local variables. The C language does not force a local variable to be assigned an initial value, and the value of the local variable is indeterminate until the programmer assigns the initial value to the local variable, because the variable allocates memory for each time the function is called, and the value in the reallocated memory is indeterminate.

Global variables: Also known as external variables, all functions in the source file can use global variables. If the local variable has the same name as the global variable, the local variable overrides the global variable . Global variables are also divided into external global variables (variables that are accessible to all source file functions, declared with the extern keyword or not), and internal global variables (variables that are accessible by all functions in the current source file, declared with the static keyword).

5, C language program running memory can be divided into three parts: program area, static storage area, dynamic storage area .

Static storage: Variables in the static store allocate memory at the beginning of the program run, until the end of the program frees up memory, while the static store's variables always occupy a fixed amount of memory while the program is running, and static storage always holds two types of variables: global variables, static modified local variables .

Dynamic storage: The storage space for dynamic storage is dynamically allocated, and when the program calls the same function multiple times, the function's local variables (non-static-modified variables) dynamically allocate memory space each time, and the memory space is automatically freed each time the function ends, and this allocation and deallocation is dynamic. dynamic storage includes three types of data: formal parameters of functions, non-static local variables, field data for function execution, and return addresses .

6. The storage category of variables in C language:

Auto: Automatic storage of a few, local variables by default to take this storage mechanism

Static: Specifies that the local variable is stored in a static store so that the space occupied by the variable loves you until the program exits.

Register: Specifies that the variable is stored in a register.

extern: Defines an external local variable.

1 #import<Foundation/Foundation.h>2 3 //understand the difference between auto and static4 voidFacintN)5 {6AutointA =1 ;7     Static intb =1 ;8a+=N;9b+=N;Tenprintf"A =%d, B =%d", A, b);  One } A  - intMainintargcChar*argv[]) - { the     //call three times with a loop -      for(inti =0; I <3; i++) -     { - FAC (i); +     } - } +  A /*The output result is at a = 1, b = 1 - A = 2, B = 2 - A = 3, B = 4 - */  

7, pre-processing is C, OC Special command. Before the compiler compiles the program, the compiler processes the preprocessing and compiles the results of those preprocessing with the source program.

Two characteristics of preprocessing: 1, pre-processing commands must start with # 2, preprocessing is usually at the beginning of the program.

1 //Use # define to perform macro definitions, #undef取消宏定义2 #defineYES 13 #definePI 3.14159264 5 #undefYES6 7 //with parameter macro definition8 #defineArea (R) pi*r*r9 Ten // C language uses # include to import other source programs One // OC Language using #import to import other source programs A  - //use #ifdef, #ifndef, #else, #endif执行条件编译 - #ifdefMacro Name the     //Statement - #else -     //Statement - #endif +  - //perform conditional compilation using # If, #elif, #else, #endif + #ifAn expression A     //Statement at #elifAn expression -     //Statement -     //can have multiple #elif statements - #else -     //Statement - #endif

The function of OC Learning 2--c language characteristics

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.