C/C++|Introduction to Internet of Things Development + Project Combat|Macro Definition|Data Declaration|Bit Operation|Type Modifier|Access Fixed Memory Location|Advanced Embedded C Language|Explanation of Common Interview Questions-Study Notes (13)


Reference: Wheat Academy - Embedded C Language Advanced - Use of C Language Functions - Explanation of Common Interview Questions
Reference : 0x10 Basic Questions Embedded Programmers Should Know

Explanation of common interview questions

macro definition

1. Use the preprocessing directive #define to declare a constant to indicate how many seconds there are in a year (ignoring the leap year problem) #define
macro name macro body macro
name: uppercase letters indicate
#define SECOND_OF_YEAR 123456 //Not recommended
#define SECOND_OF_YEAR ( 365 24 3600)UL //Constant expression, calculated at compile time, needs protection

The constant range of INT is
char 8bit, 0-255 up to 256 and needs a suitable box, so add L, unsigned plus U
16 bit, 0-65535 up to 65536
int a = SECOND_OF_YEAR;

data statement

Use variable a to give the following definition
a) an integer <An integer)
int a;
b) a pointer to an integer (A pointer to an integer)
int *a;
c) a pointer to a pointer , the pointer it points to is an integer (A pointer to a pointer to an integer)
int **a;
d) an array of 10 integers <An array of 10 integers)
int a[10];
e ) an array of 10 pointers pointing to an integer. (An array of 10 pointers to integers)

int* a[10] // *修饰10,每一个元素的属性,是个地址,[]修饰a,右侧优先级高,a升级为数组标签,a的左侧变为对存放内容的修饰,int修饰*指向整形数

f) A pointer to an array of 10 integers (A pointer to an array of 10 integers)

int[10]*a  ----->int (*a)[10]

g) A pointer to a function that has an integer parameter and returns an integer (A pointer to function that takes an integer as an argument and returns an integer)

int fun(int) *a----->int (*a)(int)

h) An array of ten pointers to functions that take an integer argument and return an integer int (*a
[ 10]) (int)
insert image description here

Summary of use of type modifiers

What is the function of the keyword static?
1. Modify local variables;
default local variables exist in the stack space and have a short lifetime;
local static, local variables are saved in the data segment, and the program ends only after the end of the program
2. Modify global variables;
compile When preventing renaming (multiple names), it only works in this file.
3. Modify the global function.
Prevent renaming (multiple names) when compiling, and only work in this function.
What does the keyword const mean?
C: read-only, suggestive, not mandatory, not a constant, try not to modify, pointers, out-of-bounds, and overflow Can modify
C++:
What does the constant keyword volatile mean? And give three different examples
Prevent C language compiler optimization, the modification of this variable may be modified by a third party

bit manipulation

Embedded systems always require the user to perform bit operations on variables or registers.
Given an integer variable a, write two pieces of code, the first one sets bit 3 of a, and the second one clears bit 3 of a. In the above two operations, keep the other bits unchanged.
Setting is set 1, clearing is clear 1.
unsigned int a;
a|=(0x1<<3);
a &=~(0x1<<4);

Access to a fixed memory location

In a project, it is required to set the value of an integer variable whose absolute address is 0x67a9 to 0xaa66. The compiler is a pure ANSI compiler. Write code to accomplish this task.
Mode 1:
int *p = (int *)0x67a9;
p[0] = 0xaa66;
Mode 2:
*((int )0x67a9) = 0xaa66;
Mode 3:
((void(
)(void))0x67a9)();

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/130320638