1. C language is confused
1. Operator – bitwise inverse (~)
Question: Why print -1? Answer:
- In 32-bit operating systems, integers hold 32 bits
Int a = 0
00000000000000000000000000000000
11111111111111111111111111111111 ~a
-
There are three binary representations of an integer, namely source code, inverse code and complement code. Integers, on the other hand, store complement in memory.
1:
The original code: 1000000000000000000000001
(The first bit is a sign bit, and 1 represents a negative number)
First, convert the original code into inverse code (the symbol bit remains unchanged, and the rest is reversed by bit) :
Radix-minus-one complement: 1111111111111111111111110
Complement: 1111111111111111111111111
-
Computer print is the original code, so the complement of 1111111111111111111111111
It prints out as minus 1.
-
2. static
- Static modifiers a global variable that can only be used in its own source file.
- Global variables can be used inside other source files because they have external link properties, but when modified static, they become internal link properties, and other source files cannot link to the static global variable.
3. Write programs in modules
We recommend putting function declarations in header files and function definitions in source files. If you are writing addition functions, you need to create add.h header files and add.c source files
Add. H file to store function declaration:
int Add(int x,int y);
Copy the code
Add. C file storage function definition:
int Add(int x,int y)
{
return x+y;
}
Copy the code
Then include the add.h file in the main program and use it:
#include <stdio.h> #include "add.h" int main() {printf("%d", add (3,2)); return 0; }Copy the code
4. Static library (.lib) generation and use
Programmer A wants to sell the subtraction function Sub() to programmer B, but does not want to expose the source code to B, so A generates the program as A static library:
-
Right – click Solution and select Properties at the bottom
In Configuration Properties, select Static Library (.lib) for Configuration type, then click Apply and confirm.
CTRL +F5 to generate it. The “Debug” folder is found in the upper directory of the program, where the static library is located.
Then programmer B can include sub. h file in the program and import the static library in the main program:
\