First, the function
(a) what is a function
Any C language program is composed of one or more program segments (small programs), each of which has its own function, which we generally call "functions".
(ii) Definition of function
Purpose: To encapsulate a commonly used function to facilitate later invocation.
Step: Determine the function name, determine the function body, call
Format: Return value type function name (formal parameter list)
{function Body}
(c) Invocation of functions
1 intAverageintNUM1,intnum2)2 3 {4 5 return(NUM1 + num2)/2;6 7 }8 9 intMain ()Ten One { A - intA =1; - the intb =3; - - intc = Average (A, b);//calling Functions - + return 0; - +}Example:
(d) Parameters of the function
①. Parameters: When defining a function, the arguments following the function name, such as NUM1 and num2
②. Arguments: When a function is called, the specific data of the descendant, such as A and b
③. The number of formal parameters and arguments must be equal
④. A variable with the same name as the parameter cannot be defined inside the function body
⑤. If the basic data type is a formal parameter, it is purely a value pass, and modifying the value of a function's internal parameter does not affect the value of the argument
⑥. A function can have no formal parameter, or it can have an infinite number of parameters
(v) return value of function
The role of 1.return:
①. Exit function
②. Returns a specific value to the caller of the function
2. Return value Use Note:
①. C is a weakly grammatical, weak type of language, very not strict
②. If the type of the return value is not written clearly, the default is int type
③. void represents 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
(vi) Function use note
①. Duplicate function name not allowed by default
②. Functions are equal and cannot be nested defined
③. A function cannot be defined repeatedly, but can be declared repeatedly, as long as it is declared before the call, where it can be
④. function if the declaration is not defined, then the compilation succeeds, but the link is invalidated
(vii) Collaborative development of multiple people
#include <stdio.h> is a system header file.
#include <abc.txt> is equivalent to copying all 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, and you can use a relative path or an absolute path. The left side has an absolute path, and no/is a relative path.
Note: Custom files use "" and system files use <>.
If the function definition is called after the call and no corresponding declaration is made, the compiler will make an error.
Call function, no function declaration, compiler will not error, link times wrong, do not define the function instead of error.
Link: Combine the relevant. o Target files in the project with the C language libraries to generate the executable file.
Usually:
①. The definition of a function is placed in a. c file, and the declaration of the function is placed in the. h file
②. If you have a function defined in a. c file, you only need to include the. h file that corresponds to the. c file.
③. h file is born soy sauce is copied by others to contain, compile the link without writing
(eight) complement of functions
(1) Main function
The return value is returned to the operating system, 0 is normal exit, and the other value is abnormal exit.
(2) printf function
The return value is the number of characters. such as printf ("abc"); The return value is 3, if printf ("ABC man \ n"), the return value is 7 because a Chinese is 3 characters in the Mac OS X system.
(3) scanf function
①. # include <stdio.h> must be added before use.
②. When calling the scanf function, you need to pass in the address of the variable as a parameter, and the scanf function waits for input data and assigns the input data to the variable that corresponds to the address.
③. When you receive multiple values with the scanf function, the delimiter between the values is arbitrary.
Do not include \ n in the first parameter of ④.scanf, such as scanf ("%d\n", &a); This causes the SCANF function to not end.
Second, the operation
(i) Arithmetic operations
There are 34 types of operators in C, including common subtraction operations
①. Addition: + can also indicate a plus sign
②. Subtraction:-can also represent a minus sign
③. Multiplication: * Non-mathematical meaning X
④. Division:/Note A value of 1/2 is 0, not One-second
⑤. Residual (modulo operation): the remainder after dividing two integers (note that both sides must be integers, and the positive and negative depend on the value of the left of%)
Attention:
①. int a = 10.8;//loss of precision. Result for 10--automatic type conversion
②. int a = (int) 10.8;//cast 10.8 to integral type--forced type conversion
③. Double C = 10.6 + 6//result is 16.600000. In the computer, take two numeric values for operation, and automatic type promotion if different types. That is, convert 6 to 6.000000 and then participate in the operation-automatic type promotion
(ii) Assignment operations
①. Simple Assignment
int a = ten + 5;a = b = 10;//Right-to-left, left cannot be constant
②. Compound Assignment
A = a + 5; + A + = 5;
A = a * 5; = a *= 5;
A + = 5 * 6 + 4; + A = a + (5 * 6 + 4)
Tip: The order of operations depends on the precedence of the operators and the order in which they are combined.
(iii) Self-increase self-reduction
int a = 10;
There are four ways to add the value of a to 1:
①. A = a + 1;
②. A + = 1;
③. a++;
④. ++a;
Simple distinction between a++ and ++a.
int b;
int a = 10;
b = ++a; A = = 11,b = = 11;a First add a value of 1 to 11, and then assign a value to B
b = a++; A = = 11,b = = 10;a First copy the value 10 to b,a yourself plus 1 for 11
b = (a++) + (++a); A1 = = 10,a2 = = 12,b = 22
b = (++a) + (a++); A1 = = 11,a2 = = 11,b = 22
Note: int d = 10++;//is wrong because it makes no sense to self-add and decrement constants.
(d) sizeof
Function: The number of bytes of memory used to calculate a variable, constant, or a data type.
Basic form: sizeof (variable name | constant | data type), returns a value after completion.
①. Sizeof Variable | constant
②. Sizeof (Variable | constant)
③. Sizeof (data type)
Note: Data types must be enclosed in parentheses and cannot be written in the form of the sizeof data type.
(v) Relational operations
(1) Condition judgment
By default. The correct code for each sentence written in the program is executed, but many times we need to execute a certain piece of code, such as a login operation, in a condition that can be done using conditional statements.
(2) True and False
In C language, the condition is called "true", the condition is not established is called "false".
C language provisions, any value is true and false, any non-0 value is true, only 0 is False , no Boolean type.
(3) Comparison of relationships
The result of the relational operation is only two cases, if the condition is true, the value is 1, if the condition is false, the value is 0. There are 6 types of operators, namely: (1) < (2) <= (3) > (4) >= (5) = = (6)! =
(4) Use note
①. = = and! = have equal precedence, and the other four relational operators have equal precedence, and the precedence of the former is lower than the latter. such as 2 = = 3 > 1 should be calculated first 3 > 1
②. In relational operators, if the precedence is the same, then " left to right " is combined. For example, 4 > 3 < 2, calculate 4 > 3 First
③. The precedence of the relational operator is lower than the arithmetic operator. such as 3 + 4 > 8-2 equivalent (3 + 4) > (8-2)
④. Exercise 5! = 4 + 2 * 7 > 3 = = 10 Calculate First 5! = > 3 = = 10,5! = 1 = = 10,1 = = 10, False
(vi) Logical operation
The result of a logical operation is only two: true (0) and False (1)
①. Logic and && conditions 1 && Condition 2 only the condition 1 and Condition 2 are established, the true or false.
②. Logic or | | Condition 1 | | Condition 2 When one of the conditions 1 or Condition 2 is true, it is false when it is not established.
③. Logical Not! ! Conditional inversion
Note: When a logical operator is operating, the following conditions are ignored, as long as the integrity of the whole can be determined.
int a = b = ten;
int c = (A > 5) && (++b >= 11);//At this time a = = 10,b = = 11,c = 1
int C = (A < 5) && (++b >= 11);//At this time a = = 10,b = = 10,c = 0
tip: You can use () when you are doing a logical operation, if you don't know the priority of each symbol.
(vii) Three mesh operation
Binocular: Requires two values to participate in the operation
Monocular: A numeric participation operation such as!5
Trinocular: Requires 3 values to participate
Format: Condition? Value 1: Value 2
Judge first? Before the conditions, if the condition is set to return a value of 1, if the condition is not established, return to the condition 2.
A and B are required to be compared, and the values are stored in c = a > B. A:b
Compares the value of the A,b,c three number and deposits the maximum value in D
int Abmax = (a > B)? A:B;
D = abmax > c:abmax:c;
"iOS Development Fundamentals" "C language" 02-Functions, operations