[IOS development Basics] [C Language] 02, ios development 02

Source: Internet
Author: User

[IOS development Basics] [C Language] 02, ios development 02

 

I. Functions

(1) What is a function?

Any C language program is composed of one or more program segments (applets), each of which has its own functions. We generally call these segments "functions ".

(2) Function Definition

Objective: To encapsulate a common function for future calls

Step: determine the function name, function body, and call

Format: Return Value Type Function Name (formal parameter list)

{Function body}

(3) function call

1 int average (int num1, int num2) 2 3 {4 5 return (num1 + num2)/2; 6 7} 8 9 int main () 10 11 {12 13 int a = 1; 14 15 int B = 3; 16 17 int c = average (a, B); // call function 18 19 return 0; 20 21}Example:

(4) function parameters

①. Form parameter: when defining a function, the parameters following the function name include num1 and num2.

②. Real parameters: When a function is called, the specific data transmitted by the person is as follows: a and B

③. The number of form parameters and real parameters must be equal.

④. The function body cannot define variables with the same name as the form parameter.

⑤ If the basic data type is used as the form parameter, it is purely a value transfer. Modifying the value of the form parameter in the function does not affect the value of the real parameter.

6. A function can have no form parameter or an unlimited number of form parameters.

(5) return values of functions

1. Function of return:

①. Exit the Function

②. Return a specific value to the caller of the function.

2. Pay attention to the returned values:

① C language is a language with weak syntax and is very non-strict

②. If the type of the returned value is not clearly written, the default value is int type.

③. Void indicates no return value

④. Even if the return value type is explicitly declared, no value can be returned.

⑤ C language by default, two functions with the same name are not allowed

(6) Notes for using functions

1. Duplicate function names are not allowed by default.

②. Functions are equal and cannot be nested.

③ Functions cannot be defined repeatedly, but can be declared repeatedly, as long as they are declared before the call.

④. If the function is declared as long as it is not defined, the compilation can be successful, but the link will become invalid.

(7) Collaborative development by multiple people

# Include <stdio. h> is a system header file.

# Include <abc.txt> is equivalent to copying all the content in the abc.txt file to the current location in plain text mode.

"" Indicates that the file is in the same path as the. c file. You can use a relative path or an absolute path. The absolute path is available on the left, and the relative path is not available.

Note: Use "" For custom files and system files <>.

When a function is called, if the function is defined after the call and no declaration is made before, the compiler reports an error;

If a function is called without a function declaration, the compiler will not report an error. If a function is not defined, the compiler will not report an error.

Link: Combine the related. o target file in the project with the C function library to generate an executable file.

Generally:

① Put the function definition in the. c file, and put the function declaration in the. h file.

②. If there is a function defined in the. c file, you only need to include the. h file corresponding to the. c file.

③. When the H file is generated, it is copied and included by others for soy sauce. It is not required to compile the link.

(8) function supplement

(1) main Function

The return value is returned to the operating system. 0 indicates normal exit, and other values indicate abnormal exit.

(2) printf function

The return value is the number of characters. For example, printf ("abc"); returns 3. If printf ("abc male \ n"); returns 7, because a Chinese character occupies three characters in Mac OS X.

(3) scanf Functions

① Before use, you must add # include <stdio. h>.

②. When the scanf function is called, the address of the variable needs to be input as the parameter. the scanf function will wait for the input data and assign the input data to the corresponding variable of the address.

③ When multiple values are received using the scanf function, the separators between values are arbitrary.

④. Do not include \ n in the first parameter of scanf, for example, scanf ("% d \ n", & a); this will cause the scanf function to end.

 

Ii. Operation

(1) arithmetic operations

There are 34 operators in C language, including common addition, subtraction, multiplication, division, and so on.

① Addition: + can also represent the positive number

②. Subtraction:-can also represent a negative number.

③ Multiplication: * Non-mathematical X

④ Division:/Note that the value of 1/2 is 0, not 1/2.

⑤. Remainder (modulo operation): the remainder after the division of two integers (note that both sides must be integers, and the positive and negative values depend on the value on the left of %)

Note:

①. Int a = 10.8; // loss of precision. Result 10 -- Automatic type conversion

②. Int a = (int) 10.8; // converts 10.8 to an integer type forcibly.

③. Double c = 10.6 + 6 // The result is 16.600000. In a computer, two numeric values are used for calculation. If the types are different, the types are automatically upgraded. Convert 6 to 6.000000 and then participate in the operation -- Automatic type upgrade

(2) value assignment

①. Simple assignment

Int a = 10 + 5; a = B = 10; // The value ranges from right to left and cannot be a constant on the left.

②. Compound assignment

A = a + 5; => a + = 5;

A = a * 5; => a * = 5;

A + = 5*6 + 4; => a = a + (5*6 + 4)

Tip: the operation order depends on the operator priority and the combination order.

(3) Auto-increment and auto-Increment

Int a = 10;

There are four ways to add the value of a to 1:

①. A = a + 1;

②. A + = 1;

③. A ++;

④. ++;

Distinguish a ++ and ++.

Int B;

Int a = 10;

B = ++ a; // a = 11, B = 11; a first adds 1 to 11 and then assigns B

B = a ++; // a = 11, B = 10; a first copies the value 10 to B, and a himself adds 1 to 11.

B = (a ++) + (++ a); // a1 = 10, a2 = 12, B = 22

B = (++ a) + (a ++); // a1 = 11, a2 = 11, B = 22

Note: int d = 10 + +; // This method is incorrect, because it does not make sense to perform auto-increment and auto-subtraction on constants.

(4) sizeof

Function: used to calculate the memory bytes occupied by a variable, constant, or data type.

Basic Form: sizeof (variable name | constant | data type). After completion, a value is returned.

①. Sizeof variable | constant

②. Sizeof (variable | constant)

③. Sizeof (data type)

Note: The data type must be enclosed in parentheses and cannot be written as a sizeof data type.

(5) relational operations

(1) condition judgment

By default. Every correct code written in the program will be executed, but most of the time we need to execute a piece of code, such as the login operation, when a condition is true, in this case, conditional statements can be used.

(2) true and false

In C, the condition is true, and the condition is false ".

The C language specifies that all values are true and false, and any non-0 values are true. Only 0 values are false and no boolean type.

(3) relationship comparison

There are only two cases of relational calculation results. If the condition is true, the value is 1. If the condition is not true, the value is false and the value is 0. There are 6 operators: (1) <(2) <= (3)> (4) >=( 5) = (6 )! =

(4) usage notes

①. = And! = The priority is equal. The priority of the other four Relational operators is equal, and the priority of the former is lower than that of the latter. For example, 2 = 3> 1 should be calculated first 3> 1

②. In Relational operators, if the priority is the same, it is "from left to right. For example, 4> 3 <2, calculate 4> 3 first

③ Relational operators have a lower priority than arithmetic operators. For example, 3 + 4> 8-2 is equivalent to (3 + 4)> (8-2)

④. Exercise 5! = 4 + 2*7> 3 = 10 calculate 5 first! = 18> 3 = 10, 5! = 1 = 10, 1 = 10, false

(6) logical operations

There are only two logical operations: true (0) and false (1)

①. When both the logical and & condition 1 & condition 2 conditions 1 and 2 are true, otherwise they are false.

②. Logic or | condition 1 | condition 2 is true when one of condition 1 or condition 2 is true, and neither of them is false.

③. The logic is not! ! Condition Inversion

Note: When performing operations on logical operators, as long as they can determine the overall authenticity, the subsequent conditions will not be executed, but will be ignored directly.

Int a = B = 10;

Int c = (a> 5) & (++ B> = 11); // at this time a = 10, B = 11, c = 1

Int c = (a <5) & (++ B> = 11); // a = 10, B = 10, c = 0

Tip: if you do not know the priority of each symbol during logical operations, you can use ().

(7) arithmetic operations

Binary: two values are required for calculation.

Single Object: one value is involved in the calculation, for example! 5

Category 3: three values are required.

Format: condition? Value 1: Value 2

Judge first? If the condition is true, 1 is returned. If the condition is not true, 2 is returned.

It is required to compare the values of a and B and store the large values in c. c = a> B? A: B

Compare the values of a, B, and c to save the maximum value to d.

Int abmax = (a> B )? A: B;

D = abmax> c: abmax: c;

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.