C/C++ | Introduction to Internet of Things Development + Project Combat | Advanced Embedded C Language | Common Keywords and Operator Operations in C Language - Study Notes (8)


Reference: Wheat Academy - Advanced Embedded C Language

2-2: Common keywords and operator operations in C language

[Key]
Master the common key words and application scenarios of C language, and use skills

keywords

Compiler: There are 32 character strings with a certain meaning defined in advance.

  1. The sizeof
    keyword, the compiler shows us one of the memory space capacity, the space that has been allocated when it is defined.
#include <stdio.h>

int main()
{
    int a;

    printf("the a is %d",sizeof a);
    // printf("the a is %d",sizeof(a));

    return 0;
}

Return value: the a is 4
characters occupy 4 bytes of memory space (32 bits).
2. Return return value
How does C language describe the attributes of these resources?
Resource usability [size]: data type, determined by the compiler to
limit the size of memory (land), keyword
3. data type
operation object: resource/memory{ Resources of memory type, LCD cache, LED lights} The minimum unit of
char hardware chip operation: bit 1 0 high and low bits The minimum unit of software operation: 8bit ==1B byte byte 4M 4Mbit Kbit/s KB/S char a; bit a ; Application scenario: the smallest unit of hardware processing char buf[xx]; int buf[x]; ASCII code table 8bit box can represent state 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024



















8bit == 256
overflow problem: char a =300; a++ 301
int
size; generally not fixed, it depends on the compiler. TC2.O is
the optimal processing size of the 2B compiler:
the maximum processing unit that can be accepted by the system in one cycle, int
32bit 4B int can just cover the expressway of one cycle with
16bit 2B int 16-bit microcontroller

int a;      char a;
int处理的数据量比char大,而且能够达到系统的最优处理大小,计数器,循环等;
char更多的是面向硬件开发。

=============================
Integer constant (a string of numbers, int size)
char a = 300; 300l 300L
2B 65535
int a = 66535; //More than 2B, must use 4B
base to represent
10
decimal octal hexadecimal binary
man and man binary derivative
101010
3bit octal
111 0x7
1000 0x8 //3 bits are 7 cycle
int a = 010 ; //Start with 0, octal represents decimal: 8

12 001 010

4bit hexadecimal int a=0x10; //Start with 0x, hexadecimal represents decimal: 16

long, short
is a special length limiter, short means hexadecimal in the 32-ary system, and it is only used when the length is strictly required.
unsigned, signed
unsigned: data
signed:
number The highest byte of the memory space is the sign bit or data

unsigned int a;
char a;

float.double
size
float 4B
double 8B
memory existence form
0x10 16
0001 10000 16

16
0001 0000 16

1.0
0001 0000 (X)

Floating point constant
1.0 1.1 double
1.0f float

void

void a;//Apply for variable name, declare flag

  1. The memory allocation defined by the default data type
    C compiler does not conform to the form of actual resources

custom = collection of basic elements


The sum between the struct
elements is accumulated struct myabc{ unsigned int a; unsigned int b; unsigned int c; unsigned int d; };




int i;
struct myabc mybuf;

order required
insert image description here

Union
shared starting address, technical code

union myabc{
char a;
int b;
};

union myabc abc;
enum
enum
enumerate--list

collection of named integer constants

#define MON 0
#define TUE 1
#define WED 2

Equivalent to: enum abc{MOD=0,TUE,WED}

enum enumeration name {constant list}; constant collection,

enum week{
Monday = 0 ,Tuesday =1,Wednesday = 2,
Thursday,Friday,
Saturday,Sunday
};
insert image description here

Sample code:

#include <stdio.h>
//enum abc{MOD=100,TUE,WED};
enum abc{MOD=100,TUE,WED};

int main()
{
    //enum abc a1 = MOD;

    enum abc a1 = 800; //语义限制符,不强制,不在区间内也能编译,

    printf("the a1 is %lu:%d\n",sizeof(a1),a1); //取3种里的任意一种

    printf("the a is %d\n",WED);

    return 0;
}

Aliases for typedef
data types
int a = 170;
int b = 3600;
len_t a = 170;
time_t b = 3600;

int a; //a is a variable of type int
typedef int a_t; //a is a nickname of type int
a_t mysize;

xxx_t:typedef

  1. Logical structure
    CPU executes program sequentially

Branch - "Select

cycle

if、else

switch{integer variable}
case 1:
break
default
multi-branch

do, while, for
for: number of times
while: condition, loop in a certain environment

continue, break, goto
The goto function jumps internally, and cannot jump between different functions
4. Type modifiers
limit the storage location of memory resources

Restrictions on Positions in Resource Properties

auto
default -------> the allocated memory is readable and writable area
auto int;
auto long b;
if the district city is within {}, the representative position is in the stack space
register
register int a;
limit variables are defined in The modifier on the register is mainly to tell others that a is accessed more frequently.
Define some fast access variables,
the compiler will try to arrange the register area of ​​the CPU to store, if there are not enough registers, a is still placed in the memory.
The symbol & does not work for register, it is illegal and cannot tell the address

Memory (memory) Register (CPU buffer)
0x100 R0, R2, R5

Sample code:

#include <stdio.h>

int main()
{
    register int a;

    a = 0x10;

    printf("the a is %d\n",&a);

    return 0;
}

return value:

E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
2.c: In function 'main':
2.c:9:5: error: address of register variable 'a' requested

static
Static
application scenarios:
1), modify 3 kinds of data
int fun()
{ int a;====>static int a; } 2), variable outside the function int a; =====>static int a; int fun() {





}
3)、函数的修饰符
int fun();======>static int fun();

The definition of const
constant C language weakness
Read-only variables cannot be modified explicitly. Pointers can be modified, memory leaks.
const int a = 100;
extern
external declaration, global variable function, contrary to static,
volatile
is a keyword that informs the compiler of the compilation method, does not optimize compilation,
has a large relationship with the address, and is commonly used in embedded bottom layer, single-chip microcomputer, and driver development

int a = 100;

while ( a==100 );

mylcd();


[a] : address of a

f1:LDR R0,[a]

f2:CMP r0,#100

f3:JMPeq f1 ----->JMPeq f2 //The compiler may directly execute f2 after optimization, leaving out f1

f4:mylcd();

  1. operator

Arithmetic operation operation
+, -
, /
If the CPU performance does not support, the portability will be
poor . accomplish. int a = b+10; CPU can process %
in one cycle : 0% 3=0 1%3 = 1 2%3=2 3%3=0 4%3=1 … . %n = res[0 - m- 1] Take a range of numbers: eg. Give an arbitrary number, get a number within 1 to 100? ( m % 100 )+1 =====>res; get a one-digit cycle in the M base Subscript logical operation of data structure true, false, return result 1 0 int a = -1; if(a)











||, &&
positions cannot be exchanged
A || B —> B
||
Example:

#include <stdio.h>

int main()
{
    int a = 10;

    int res;

    // res = (a == 10) || printf("==============\n"); //如果a==10为真,则以后没必要执行

    res = (a != 10) || printf("==============\n");

    printf("the main is %d\n",res);

    return 0;
}

>,>=,<,<=
! Logical inversion
?: Ternary
bit operation
<<,>>
Left shift: Multiplication*Binary shift
mm<<1; m 2
mm<<\n; m
2* no

[Data, number]
-1 *2 -2:
8bit
100000001 //Original -1
111111110 //Automatic inversion
111111111 === -1 Add the original and inversion to get the actual stored -1

1000 0010
1111 1101
1111 1110 ==== -2 // Shift to the left to make up 0, shift to the left and multiply by 2

Right shift: related to symbolic variables, strictly follow the operator
int a; a>>n;
usigned int a>>n;
infinite loop example:

#include <stdio.h>

int main()
{
    int a = 10;

    // int a = -10; //补位都是1,进入死循环

    while(a){
        a = a>>1;
    }

    printf("++++++++++\n");

}

&,|,^
and, or, XOR
&, |
A & 0—>0
&: mask
int a = 0x1234;
a & 0xff00; mask the lower 8 bits, take out the upper 8 bits
A & 1->A
& take out and clear clr
|:
A | 0 ==== A;
reserve
A | 1 ====== 1
set to high level, set set

Set bit5 of a resource to high level, other bits remain unchanged
int a;
a = a | 1 0 0 0 0 0 ; a = (a |(0x1 <<5)); =====> a | ( 0x1 << n) ;

Clear the fifth bit
int a;
a = a & 011111 31 a & 31 31:32bit
a = a & ~(0x1<<5)======>a = a & (~(0x1<< n)) ;
^,~
,~
1^1=0 0^0=0 1^0=1
Algorithm AES SHA1

int fun()
{
int a = 20;
int b = 30;

xXXX----->

int c;c= a;a= b;b=c;
a =a^b; b =a^b; a = a^b;
a=30
b=20
}
~
0xf0 ~ 0xffff ff0f

Thinking: We want to set the resource 456bit to 101?

Assignment operation
a|(0x1<<5)
~a
a=a+b
a+=b
a = a+b;
a+=b
al=(0x1<<5)
a &=~(0x1<<5)
6. Memory access symbol
()
qualifier (a+b)*c
function access symbol
int fun();
fun();
[]
array
memory access ID symbol a[1] a[2]
{}
function body qualifier
struct abc The definition symbol of {xxxx}
.->
Access method to custom member variable of continuous space, ->Address access
&, p
p takes address
a
10 multiplication

Guess you like

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