Detailed description of Embedded c language development and Embedded c practice
Detailed description of Embedded C Language Development
I. Overview
1. Why C language in embedded development?
Because the kernel of the operating system uses the C language, and the C language also has the following advantages:
(1) Excellent portability, able to run on a variety of Soft/hard platforms with different architecture (smaller modification volume, better portability );
(2) Concise and compact, flexible syntax mechanism, and direct access to hardware (high efficiency );
(3) high operation efficiency
Expansion:
When Will assembly be used? When will c be used? (C vs Assembly)
Assembly is a low-level language and cannot implement complex functions. Therefore:
When hardware is initialized -- Assembly
When complicated hardware operations are performed-C Language
Process-oriented processing mechanism VS object-oriented processing mechanism (details stamp URL)
Http://blog.csdn.net/wzhcalex/article/details/51878170
2. Position in Embedded Development:
(1) embedded Linux application software development engineering design;
(2) embedded Linux driver development engineer;
(3) embedded BSP development engineer;
(4) embedded Kernel (Kernel) development engineer;
3. proficient in C language assessment criteria:
(1) Enterprise pen questions;
(2) accumulated amount of code (enhanced programming training)
(3) Good coding specifications (Huawei's coding specifications );
(4) Industrial Application project experience;
4. How to learn C language (both external and internal skills)
(1) Zero-basic learning experience (how cainiao cultivate to be an old bird)
(2) develop algorithms in C Language
(3) C and pointer, C language expert programming, programmer self-cultivation, high quality C/C ++ programming, and the beauty of Programming
5. What are the C language standards?
K & RC, C89, C99, C11
Note:
C89 supported by gcc, partially compatible with C99
Different compiler standards are different
Many compilers support C89.
Ii. Data Types
1. What is a data type?
The division of data sets. Different data types have different meanings for CPU.
2. What types of data are there?
3. Left and Right rules
Right-left rule: read from undefined identifiers in the parentheses in the innermost part, and then read from the right to the left. When parentheses are encountered, the reading direction should be dropped. Once everything in the parentheses is parsed, the parentheses appear. Repeat this process until the entire declaration is parsed.
Enterprise pen questions:
1. Use variable a to define the following:
A) An integer (An integer): int;
B) A pointer to an integer (a pointer to an integer): int *;
C) A pointer to a pointer that points to an integer (A pointer to
Pointer to an integer): int **;
D) An array of 10 integers: int a [10];
E) An array with 10 pointers pointing to An integer (An array of 10)
Pointers to integers): int * a [10];
F) A pointer to an array with 10 integers (A pointer to an array of 10 integers ):
Int (* a) [10];
G) A pointer to A function. The function has an integer parameter and returns an integer (A pointer
To a function that takes an integer as an argument and returns an integer ):
Int (* a) (int );
H) An array with 10 pointers pointing to a function that has an integer parameter and returns
Returns An integer (an array of ten pointers to functions that takes An integer
Argument and return an integer): int (* a [10]) (int ).
2. int * (* fpl) (int) [10];
Fpl: function pointer variable. This function pointer points to an integer pointer, and the returned value is an array pointer;
Int * (* arr [5]) ();
Arr: array of function pointers. The elements in this array point to a function with null parameters and return values as function pointers. This function pointer points to a function with null parameters and return values as integer pointers.
Float (* B () []) ();
B: function. If the input parameter is null, the returned value is an array pointer. The input Pointer Points to a function pointer array. The elements in the array point to a function whose input parameter is null and return value is float.
Void * (* c) (char, int (*)());
C: function pointer variable. This function pointer points to a char and function pointer. the return value is void *. The parameter of this function pointer is null and the return value is int.
Void ** (* d) (int *, char ** (*) (char *, char **));
D: function pointer variable. It points to the function parameter char. The function pointer returns void **, char *, char **, and char **;
Float (* e [10]) (int *) [5];
E: function pointer array, pointing to function parameter: int *, return value: array pointer, this array element points to float;
4. implicit type conversion and forced type conversion:
Char <int <float <double implicit type conversion (automatic)
Sample Code:
This is implicit type conversion;
Forced type conversion:
Sample Code:
The char * in the above Code is forced type conversion
5. Important knowledge points of data types:
Byte Length:
Bit;
Byte = 8bit;
Half word = 2 bytes = 16 bit;
WORD = 4 bytes = 32;
6. Basic Data Types
Char 1 byte;
Short 2 bytes;
Int 4 bytes;
Long 4 bytes;
Float 4 bytes;
Double 8 bytes;
Long 8 bytes;
Code query:
Expansion:
Why are pointers of any type 4 bytes?
Because the Pointer Points to the address, the address length is fixed. The address length is determined by the operating system. If the operating system is 32-bit, the address length is 4 bytes. If the operating system is 64-bit, the address length is 8 bytes.
Char value range:
Unsigned: 0 ~ 255 (2 ^ 8-1)
Signed:-128 ~ 127
When there is a symbol: the first is the symbol bit:
0 is an integer: 0 000 0000 = 0; 0 111 1111 = 127;
1 is negative: when the number is negative, the computer saves the complement code.
1 000 0000 anti-increment 1 =-128
1 111 1111 decimal addition 1 =-1
Complement the code and Add 1 to the original code.
For example:
Print ~ 2: constants are all signed: 0 000 0010
Inverse value: 1 111 1101 negative value
Backend encryption: 0011 =-3
Code example:
Analysis:-128 0000
Anti-1 111 1111
Add 1 0 111 1111 = 127
Enterprise pen questions:
Output result analysis:
Signed: I = 127 a [127] =-128
I = 128 a [128] = 127
...
I = 255 a [255] = 0; 0 is equivalent to '\ 0'
Strlen (a) calculates the number of jumps out of '\ 0', totaling 255.
Unsigned: I = 0 a [0] = 255;
I = 1 a [1] = 254;
...
I = 255 a [255] = 0;
Strlen (a) calculates the number of jumps out of '\ 0', totaling 255.
Differences between signed data and unsigned data:
Instance resolution:
Analysis:
When comparing the number of signed symbols and the number of unsigned symbols (==,<,>,<=, >=), the number of signed symbols is implicitly converted to the number of unsigned symbols (that is, the underlying complement code) however, this number is changed from a signed number to an unsigned number.
7. Differences between sizeof and strlen (for details, visit the website ):
Http://blog.csdn.net/wzhcalex/article/details/51852632
8. Variables and constants
Three main features of variables:
Variable data type: mainly describes the memory space occupied by the variable, such as the int type;
Scope of a variable: The validity range of the variable, such as the scope of use of the variable;
Storage Class of variables: The storage method of variables in the memory. Different storage methods affect the life cycle of variables in the memory.
MMU: virtual address unit solves the problem of memory resource scarcity
(Print an address: All printed addresses are virtual addresses)
To protect data security, the operating system will divide the space:
Stack space: local variables, function parameters, and automatic variables (released after calling)
Features: Advanced and later, management permissions: System
Heap space: allocated space for malloc, ralloc, and calloc
Features: first-in-first-out, and management permissions: Users
Data Segment: bss: Save uninitialized global variables
Rodata: Constant
. Data (static data zone): global variables (released after the program ends), static modified Variables
Differences between global variables and local variables:
(Mainly from the perspective of space allocation and release of uninitialized values)
9. Declaration and definition:
Statement: no memory space is allocated. It can be declared multiple times;
Definition: allocate memory space, which can be defined only once.
There are two scenarios for variable declaration:
Definition Declaration: a bucket needs to be created. For example, int a has already created a bucket during declaration;
Reference Declaration: you do not need to create a bucket. For example, extern int a, where variable a is defined in another file.
10. format the output and input:
First, use the code to illustrate the different input and output methods for different data types:
[Html] view plaincopy
# Include
Intmain ()
{
Inti;
Intnum;
Charch;
Floatf_num;
Doubled_num;
Inta [3];
Charsrc [1, 100];
Scanf ("% d", & num );
Printf ("num = % d \ n", num );
Getchar ();
Scanf ("% c", & ch );
Printf ("ch = % c \ n", ch );
Scanf ("% f", & f_num );
Printf ("f_num = % f \ n", f_num );
Scanf ("% lf", & d_num );
Printf ("d_num = % lf \ n", d_num );
Printf ("pleaseinputa [3]: \ n ");
For (I = 0; I <3; I ++)
{
Scanf ("% d", & a [I]);
}
For (I = 0; I <3; I ++)
{
Printf ("a [% d] = % d \ n", I, a [I]);
}
Scanf ("% s", src );
Printf ("src [100] = % s \ n", src );
Return0;
} Running result:
When getting characters, use a getchar () to clear the cache
The role of getchar () is as follows (