requirement
practice
Classroom code - teacher's explanation (review!!!)
Practice in class - teacher's explanation
Homework after class - for example: 20 questions, 5 of which will be explained in class. Write the ones that will be explained first
Self study after class
C language 100 questions - do it yourself (do it if you have ideas and can understand after reading the questions)
Answer: mainly depends on ideas
Compilation of C language test questions -- practice by yourself
Answer: different from the answer, the running environment may be different
Interview questions - array interview questions
The basic concept is clear
Array pointer
Function pointer
Array of function pointers to pointer functions
Constant pointer
What you say casually in class, you can try to expand the content
The knowledge points will be answered on the same day. The knowledge points won't be spoken in three or four days. Think about it yourself
1, Explanation requirements
- Data types, operators and expressions
- Input / output section
- Process control
- array
- Pointer
- function
- Construction type
- Dynamic memory management
2, Basic knowledge
- C language features
- Concepts of programs and algorithms
3, Data type:
When creating a variable or constant, you must specify the corresponding data type, otherwise you cannot allocate memory to the variable
1. Bytes occupied
- long >= int
- char < int <= float < double
Generally, char occupies 1 byte, and other types are uncertain
2. Storage of data types
type | Sign bit | Exponential bit | Mantissa part | Total digits |
---|---|---|---|---|
float | 1 | 8 | 23 | 32 |
double | 1 | 11 | 52 | 64 |
3. Conversion between different types of data
- Implicit conversion
- Cast type
4. Binary conversion
Decimal to binary
Divide by 2, take the remainder, and the remainder is in reverse order
Hexadecimal to hexadecimal
Divide by 16 and take the remainder in reverse order
Signed
Binary itself - positive number
The absolute value binary itself takes the inverse plus 1 - negative number
The data range is different, and the number of data is the same - signed and unsigned
5.sizeof
**Function: * * use sizeof keyword to count the memory size occupied by data type
Syntax: sizeof (data type / variable)
Return value: integer / long
Example:
#include <stdio.h> #include <stdlib.h> int main() { int i = 23; printf("sizeof(int) = %ld\n",sizeof(i)); printf("sizeof(double) = %ld\n",sizeof(double)); printf("sizeof(float) = %ld\n",sizeof(float)); printf("sizeof(long) = %ld\n",sizeof(long)); printf("sizeof(char) = %ld\n",sizeof(char)); exit(0); }
6.ASCII code
Common Ascii codes:
-
NULL null character 0
-
\n 10
-
0-48~9-57
-
A-65~Z-90
-
a-97~z-122
7. Escape character
**Function: * * used to indicate some ASCII characters that cannot be displayed
At this stage, we often use escape characters: \ n \ \ t
Escape character | meaning | ASCII value (decimal) |
---|---|---|
\a | alert | 007 |
\b | Backspace (BS) to move the current position to the previous column | 008 |
\f | Page feed (FF) moves the current position to the beginning of the next page | 012 |
\n | Line feed (LF) moves the current position to the beginning of the next line | 010 |
\r | Press enter (CR) to move the current position to the beginning of the line | 013 |
\t | Horizontal tabulation (HT) (skip to next TAB position) | 009 |
\v | Vertical tabulation (VT) | 011 |
\\ | Represents a backslash character '' | 092 |
' | Represents a single quotation mark (apostrophe) character | 039 |
" | Represents a double quote character | 034 |
? | Represents a question mark | 063 |
\0 | Number 0 | 000 |
\ddd | Octal escape character, d range 0 ~ 7 | 3-bit octal |
\xhh | Hexadecimal escape character, h range 09, af, A~F | 3-bit hex |
Special: \ ddd \xhh
4, Constant
1. Integer constant
Example: 12 123
2. Real constant
Example: 123.456 3.14
3. Character constant
Concept: a single character or escape character enclosed in single quotation marks
Example: 'a' \ 012 '\ n' \ XFF '
Note: '\ 018' / / octal cannot have 8
4. String constant
Concept: zero or more characters or escape characters enclosed in double quotation marks
Example: "ab" "abcd" "ab cd" ""
5. Identification constant
#define macro name and macro body
5, Variable
life cycle
Scope
Usage: [storage type] data type identifier
auto int i = 1;
1. Storage type
Where is the storage space allocated to variables
Storage types can be omitted when defining variables
Storage type | meaning |
---|---|
auto | Variables are stored on the stack (default: random values) |
static | It is inherited from the definition to the end of the whole program (default: the value is 0) |
register | Put the storage space of variables in registers (unable to address, obsolete) |
extern | Expand the scope of a variable (from the declared position to the end of the program) |
Example 1:
#include <stdio.h> #include <stdlib.h> extern int k; int main() { static int i = 0; int j = 20; { int j = 1; printf("i = %d\n",i); printf("j within = %d\n",j); } printf("j Outside = %d\n",j); printf("k = %d\n",k); exit(0); } int k = 300;
Example 2:
#include <stdio.h> #include <stdlib.h> void sayhi() { printf("HI\n"); } void func() { int i = 1; static int j = 1; i = i+1; j = j+1; printf("i: %p --> %d\n",&i,i); //%p print address printf("j: %p --> %d\n",&j,j); } int main() { // sayhi(); func(); func(); func(); exit(0); }
Tips:
extern -- from the declared position to the end of the program
Global variable - from the defined position to the end of the program
2. Data type
- Basic type
- Construction type - array
- Pointer type
- void
3. Identifier
Definition: it is composed of numbers, letters and underscores. It cannot start with numbers. Try to see the meaning of the name
4. Value
Match its data type
6, Operator
**Function: * * used to execute code operations
1. Arithmetic operator
Function: used to process four operations
Arithmetic operators include the following symbols:
operator | term | Example | result |
---|---|---|---|
+ | Plus sign | +3 | 3 |
- | minus sign | -3 | -3 |
+ | plus | 10 + 5 | 15 |
- | reduce | 10 - 5 | 5 |
* | ride | 10 * 5 | 50 |
/ | except | 10 / 5 | 2 |
% | Mold taking (residual) | 10 % 3 | 1 |
++ | Pre increment | a=2; b=++a; | a=3; b=3; |
++ | Post increment | a=2; b=a++; | a=3; b=2; |
– | Pre decrement | a=2; b=–a; | a=1; b=1; |
– | Post decrement | a=2; b=a–; | a=1; b=2; |
Note: for modulo operation, the left and right variables involved in the operation must be integer or real
++Self increasing – self decreasing
i++ ++i => i = i+1 i-- --i => i = i-1;
Principle: variable name comes first: use variable value first, and then increase / decrease automatically
Before the symbol, increase / decrease automatically, and then use the variable value
Example:
int i = 3,j = 5,t; t = i++; => t = i;i = i+1 => t = 3 i = 4 t = --j; => j = j-1;t = j; => t = 4 j = 4 t = i++ + --j; => --j; t=i+j; i++; => j=4;t=7;i=4 t = ++i - j--;
2. Relational operators
**Function: * * used to compare expressions and return a true or false value
Comparison operators have the following symbols:
operator | term | Example | result |
---|---|---|---|
== | Equal to | 4 == 3 | 0 |
!= | Not equal to | 4 != 3 | 1 |
< | less than | 4 < 3 | 0 |
> | greater than | 4 > 3 | 1 |
<= | Less than or equal to | 4 <= 3 | 0 |
>= | Greater than or equal to | 4 >= 1 | 1 |
Example:
Result: true(1) | false(0) int i = 3,j = 5,t; i < j 1 i >= j 0 i != j 1 ++i < j 1 ++i != --j 0 i == j 0 t = (i < j) -> t = 1
3. Logical operators
**Function: * * used to return true or false values according to the value of the expression
Logical operators have the following symbols:
operator | term | Example | result |
---|---|---|---|
! | wrong | !a | If a is false, then! A is true; If a is true, then! A is false. |
&& | And | a && b | If both a and b are true, the result is true, otherwise it is false. |
|| | or | a || b | If one of a and b is true, the result is true. If both are false, the result is false. |
Note: & & | short circuit characteristics
Example:
int i = 3; i 1 !i 0 op1 && op2 1 1 1 1 0 0 0 1 0 0 0 0 int i = 3,j = 5; i < 10 && j > 2 -> 1 i != 3 && j > 0 -> 0 op1 || op2 1 1 1 1 0 1 0 1 1 0 0 0 int i = 3,j = 5; i < 10 || j > 2 -> 1 i != 3 || j > 0 -> 1 && || Short circuit characteristics int a=1,b =2,c=3,d=4,m=1,n=1; (m = a > b) && (n = c > d); m = 0 n = 1
4. Assignment operator
**Function: * * used to assign the value of an expression to a variable
The assignment operator includes the following symbols:
operator | term | Example | result |
---|---|---|---|
= | assignment | a=2; b=3; | a=2; b=3; |
+= | Plus equals | a=0; a+=2; | a=2; |
-= | Minus equals | a=5; a-=3; | a=2; |
*= | Multiply equal | a=2; a*=2; | a=4; |
/= | Division equals | a=4; a/=2; | a=2; |
%= | Modulo equal | a=3; a%2; | a=1; |
Example:
a += b => a = a+b int a = 4; a += a -= a *= a /= 4; a = 0
5. Ternary operator (conditional operator)
Usage: expression 1? Expression 2: expression 3;
If the conditions of expression 1 are met, expression 2 is executed
If the condition of expression 1 is not satisfied, execute expression 3
Example:
op1 ? op2 : op3 int a = 3,b = 5,t; t = a > b ? a : b; //Assign the large number in a and b to t
6. Comma operator
**Function: * * indicates juxtaposition
Example:
int i = 3,j = 5,t; t = i+j,j+10; t = i++,j--,i+j,500; for(i=0,j=10 ; i < j ;i++,j-- ) xxxx;
7. Cast type
Usage: (TYPE)
Example:
#include <stdio.h> #include <stdlib.h> int main() { int i; float f = 3.9; i = (int)f; printf("i = %d\n",i); printf("f = %f\n",f); exit(0); }
Note: the object to be converted cannot be forcibly converted
Interview direct attack
Define a float type so that it is equal to xx
The float type is vague. It can't be equal to a certain number = =
if(fabs(i - 3) <= 1e-3) //fabs takes the absolute value. If the result is within 0.001, it is considered to be approximately equal to 0
Expand and improve
Search for float type interview questions
Search C language priority
Read advanced programming in UNIX Environment - Chapter 7.6