C language introductory knowledge points (part of)

Most of the following content comes from Brother Peng of BitTech . I just reprinted it after taking notes, summarizing and understanding.
Since the original link cannot be written, only the original can be written, please understand

  • I can’t get the catalogue for now, let’s watch it alive

String
A collection of character elements is
also regarded as an array, which is stored in a character array. The
string ends with'\0', but it is not included in the length of the string.

char arr1[]=“1bc”;
char arr2[]={‘a’,‘b’,‘c’};
printf("%s",arr1);
printf("%s",arr2);

arr1 will print normally and get the expected abc
arr2 will print abnormally and get abc, but garbled characters may appear later.
but

arr3{‘a’,‘b’,c’,’\0’}

Will get the same effect as arr1.
%s is printed as a string, what is needed is the address of the string to be printed. It is different from %d,%c printing.
The string will be automatically added to'\0'.

Escape character

\ddd-----ddd means 1-3 octal digits.
\xdd-----dd means 1-2 hexadecimal digits.

All characters, some on the keyboard, are stored in the computer in binary format, and there is a unique decimal conversion code called ASCII code.

The strlen() function
calculates the length of a string in bytes. End with \0.
If there is an escape character in the string, it is also counted as one character, and 1.
sizeof() keyword is
used to calculate the length of the operand type, in bytes.

Bit operator

& Bitwise and

Operate the binary bits of the operand number.

int a=5;
int b=3;
a----0101
      &
b----0011
---->0001  

In the binary digit, only if both of them are 1, can it be 1.
Analogous to &&, two truths are true and false is false.
As long as there are 0s in the two binary digits, it is 0. Please make all 1s.

| Bitwise OR

int a=5;
int b=3;
a----0101
      |1
b----0011
---->0111

In binary digits, as long as one of them is 1, the result is all 1, and only if all are 0, can we get 0.
Analogous to ||, there are true for true and all false for false.

^ Bitwise XOR

int a=5;
int b=3;
a----0101
      &
b----0011
---->0110

In the binary bit, only two bits are the same, it is 0, if the two bits are different, it is 1.
As long as the number in the bit is the same, even if both are 1, the result is 0.

~ Bitwise negation

int a=5;
a-----0101
~a----1010

Retrieve all the binary bits of the number,
0–>1
1–>0
can also be used in the judgment conditions such as

while(~scanf("%d",&a))
{
    
    }

In the computer, if the input is wrong, the requirements are not met. It returns an EOF, that is, the return value is -1.

-1
原码:10000000 00000000 00000000 00000001
反码:11111111 11111111 11111111 11111110
补码:11111111 11111111 11111111 11111111

Then invert all the bits of the complement code, including the sign bit

取反后补码:00000000 00000000 00000000 0000000

The sign bit is 0, which means it is a positive number, and the original denial is the same for a positive number. It returns a 0.

while (0) condition is false, the loop exits
and
the while (Scanf ( "% D", & A)! = the EOF)
{}
can have the same effect.

Shift operator

 >><<
>位移位运算符

<<Left shift
->> Right shift
These two operators are binary forms that act on a number.

unsigned int a=4;
int b=-4;
printf("%d",a<<2);
printf("%d",b<<2);

We all know that for positive and negative numbers, operations in memory are all performed in the form of complement. The complement of a positive number is its original code. Negative

Original code -> Inverse code -> Complement code

a
原码:00000000 00000000 00000000 00000100
反码:00000000 00000000 00000000 00000100
补码:00000000 00000000 00000000 00000100
b
原码:10000000 00000000 00000000 00000100
反码:11111111 11111111 11111111 11111011
补码:11111111 11111111 11111111 11111100

Then
move the displacement a to the left, and add zero on the right.

a<<2
00000000 00000000 00000000 00010000

The result is 4*(2^2)=16

b>>2
补码:11111111 11111111 11111111 11111111
反码:11111111 11111111 11111111 11111110
原码:10000000 00000000 00000000 00000001

The complement code is shifted to the right. Because it is a negative number, the highest bit is complemented by 1, and after subtracting 1, it is converted back to the inverse code, and then the bit is reversed, but the highest bit remains unchanged at 1.
The result is (-4)/(2^2)=-1

In this sense, the shift operator, << shift left, multiply the number (2^ the number of shifts).
->>Shift right, number/(2^number of displacement).

Unary operator

! Logical inverse

int a=!3;
int b=!0;

The result is

a=0;
b=1;

! (Logical negative) can change true (non-zero) to false (0), and can change false to true (1).

a++
++a
a–
–a

Symbol in front

-A
++a
first increase, decrease, and then use

Symbol behind

a–
a++ is
used first, then increases and decreases

int a=5;
int b=a++
int c=5;
int d=++c

The result is

a=6
b=5
c=6
d=6

Ternary operator

?
Analyzing conditions? Result 1: Result 2
If the judgment condition is true, the return value is result 1, and if it is false, the return value is result 2

a>b?10:5

If a>b, then return 10, if a<=b, then return 5.

Comma operator

int a=(2,3,4,5,6);

a==6, in the comma operator, a will only be the value of the last expression.

The content comes from Bitpeng from Xi'an Honor Bit Technology .
The blog is to deepen my memory and understanding.
Since the original link cannot be written, only the original can be written, please understand

Guess you like

Origin blog.csdn.net/weixin_52199109/article/details/112546228