Embedded software development day4

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

  1. Data types, operators and expressions
  2. Input / output section
  3. Process control
  4. array
  5. Pointer
  6. function
  7. Construction type
  8. Dynamic memory management

2, Basic knowledge

  1. C language features
  2. 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

  1. long >= int
  2. char < int <= float < double

Generally, char occupies 1 byte, and other types are uncertain

2. Storage of data types

typeSign bitExponential bitMantissa partTotal digits
float182332
double1115264

3. Conversion between different types of data

  1. Implicit conversion
  2. 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 charactermeaningASCII value (decimal)
\aalert007
\bBackspace (BS) to move the current position to the previous column008
\fPage feed (FF) moves the current position to the beginning of the next page012
\nLine feed (LF) moves the current position to the beginning of the next line010
\rPress enter (CR) to move the current position to the beginning of the line013
\tHorizontal tabulation (HT) (skip to next TAB position)009
\vVertical tabulation (VT)011
\\Represents a backslash character ''092
'Represents a single quotation mark (apostrophe) character039
"Represents a double quote character034
?Represents a question mark063
\0Number 0000
\dddOctal escape character, d range 0 ~ 73-bit octal
\xhhHexadecimal escape character, h range 09, af, A~F3-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 typemeaning
autoVariables are stored on the stack (default: random values)
staticIt is inherited from the definition to the end of the whole program (default: the value is 0)
registerPut the storage space of variables in registers (unable to address, obsolete)
externExpand 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

  1. Basic type
  2. Construction type - array
  3. Pointer type
  4. 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:

operatortermExampleresult
+Plus sign+33
-minus sign-3-3
+plus10 + 515
-reduce10 - 55
*ride10 * 550
/except10 / 52
%Mold taking (residual)10 % 31
++Pre incrementa=2; b=++a;a=3; b=3;
++Post incrementa=2; b=a++;a=3; b=2;
Pre decrementa=2; b=–a;a=1; b=1;
Post decrementa=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:

operatortermExampleresult
==Equal to4 == 30
!=Not equal to4 != 31
<less than4 < 30
>greater than4 > 31
<=Less than or equal to4 <= 30
>=Greater than or equal to4 >= 11

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:

operatortermExampleresult
!wrong!aIf a is false, then! A is true; If a is true, then! A is false.
&&Anda && bIf both a and b are true, the result is true, otherwise it is false.
||ora || bIf 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:

operatortermExampleresult
=assignmenta=2; b=3;a=2; b=3;
+=Plus equalsa=0; a+=2;a=2;
-=Minus equalsa=5; a-=3;a=2;
*=Multiply equala=2; a*=2;a=4;
/=Division equalsa=4; a/=2;a=2;
%=Modulo equala=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

Keywords: C

Added by AbydosGater on Thu, 20 Jan 2022 01:17:38 +0200