<>1.typedef: Type rename

Definition of all legal variable names ( Common variable , Pointer variable , Array variable , Function pointer , structural morphology ) Can be used typedef Convert to type name

plus typedef before
unsigned int UINT; // Common variable int* PINT; // Pointer variable int Array[10]; // Array variable void (*pfun)(
); // Function pointer struct Student stu; // Define structure variables stu struct Student *pstu;// Define structure pointer pstu
add typedef after
typedef unsigned int UINT; //UINT Type name typedef int* PINT; //PINT Pointer type name typedef int
Array[10]; //Array Array type name typedef void (*pfun)(); // Function pointer type name typedef struct
Student stu; //stu type // use stu s1; typedef struct Student *pstu; // Structure pointer type
// use :pstu p1 = NULL;
(1) Alias an existing type name
typedef unsigned char u_int8; typedef unsigned short u_int16; typedef unsigned
int u_int32; typedef unsigned double u_int64;
(2) Declaration of existing , The definition of the variable name plus typedef Become type name
#include <stdio.h> typedef int Arr[10]; int main() { Arr a = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) { printf("%4d ", a[i]); } printf("\n"); }
result :

Classical writing of structure :
#include <stdio.h> struct Student { ...; }stu, *pstu;
// This is the definition stu Structure variables and pstu Structure variable pointer // Add before typedef after typedef struct Student { ...; }stu,
*pstu; //stu It becomes a custom structure type //pstu Becomes a custom structure pointer type // This allows you to define variables using this type
<>2. Excuse me? p and q Type of
int* p, q;
result :

* Combined with variable name , Not combined with type name , therefore p yes int Pointer type ,q yes int type ;

combination 1, Want to define at the same time p and q Two pointers :
#include <stdio.h> typedef int *PTR; int main() { PTR p,q; return 0; }
result :

<>3. keyword sizeof

* sizeof Is a keyword , Determine the type and size during compilation ; #include <stdio.h> int main() { int a = 0; int x;
// Determined at compile time x = sizeof(++a); // Equivalent to x = 4; printf("a = %d\n", a); return 0; } // result a
= 0
* sizeof and strlen() Differences between
*
Different call times :sizeof Is a keyword , Type and size are determined during compilation

​ strlen() It's a function , Call function at run time

*
Different functions :strlen() Is used to calculate the length of a string ;

​ sizeof Calculate the amount of space occupied by the string ;
char buff[] = {"helloworld"}; int len = strlen(buff); //len = 10; int size =
sizeof(buff); // size = 11;
<>4, Greedy algorithm for binary number conversion

Blog

<>5,c/c++ Different emphases of constant variables

vs2019 The global variable for is uninitialized. The default is 0, Local variables are uninitialized and are random values , Compilation with this value failed

*
c Constant variables in and " variable ", Arrays cannot be defined with constants , Compile time failure ;

*
c++ Constant variables in focus on " often ", You can use this constant to define an array ;

*
C++ Constant variables are similar to macros , But there are differences and macros

* Different compilation periods
* Different from macros : Macro replacement during precompiling ; Defining arrays with constants is determined at compile time ;
* Is there a type and footprint
* Macro type does not exist , No space
* Constant variables have types , Occupied space
* Security
* Macro type does not exist , No type check , unsafe
* Constant variables have data types , Type check , Relatively safe
<>6,’ ’ and " "

’ ‘ Is the delimiter of the character , stay ’ Add before \ Post escape to single quote character –》\’

for example :
char ch = '''; //error '' Is a delimiter , You need to escape to use single quote characters char ch = '\''; //true
"" Is the delimiter of the string

<>7,ascii Code value

<>8, Escape character

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-IgEV0wBA-1635073955443)(C:\Users\ Little freak \AppData\Roaming\Typora\typora-user-images\image-20211023164550726.png)]

<>9, about 0 ‘0’ ‘\0’

int main() { char cha = 0; //ASCII Value is 0 The corresponding character is an empty character char chb = '0';
// character 0 Corresponding ASCII value 48 char chc = '\0'; // be equal to cha == 》 Null character char chd = ' ';
// space character ascii Value is 32 return 0; }
<>10, about \000 and \xff

\000 Convert octal numbers 000 Convert to decimal ascii Code value , Character corresponding to code value ;

* Valid range of octal numbers 000~377, because char Maximum value of one byte 255, Its corresponding octal number is 377; If the octal number exceeds this range, the compiler will report an error char str[]
= {"pzj\141hello"}; // Octal 141 Converted to decimal 97 //pzjahello // If it is "pzj\1411hello"
//pzja1hello // If it is "pzj\148hello" // Only escape \14 because 8 go beyond 0~7 // If it is "pzj\889hello"
// At this time \ Will be omitted int len = strlen(str); //len = 9
\x00 Hexadecimal 00 Convert to decimal ASCII Code value , Character corresponding to code value ;

*
The valid range of hexadecimal numbers is 0~ff, because char maximum 255, The corresponding hexadecimal number is ff; If the hexadecimal number exceeds this range, an error will also be reported
char str[] = {"hello\61xworld"}; //helloaworld
<>10, String and \0

String printf("%s") Print ,strcpy Copy ,strcat connect , Functions such as are in strings \0 As an end condition
char str[] = {"hello\0world"}; int size = sizeof(str); //size = 12 int len =
strlen(str); //len = 5 printf("%s\n", str); //hello
<>11, Macros and strings
#define MAX 1000 int main() { char str[] = {"helloMAX"}; printf("%s\n", str);
//helloMAX }
* reason :MAX Is part of a string , Not an identifier , Will not be replaced by macros
<>12,char ch = 'abcd’ problem

C++ There is one called “ Multi character text ” Something .'1234' This is an example . They have types int, It is implementation – Defines the values they have and how many characters they can contain .

That's nothing, directly related to the fact that characters are represented as integers , But in implementation , stand a good chance '1234' Defined as :
'1' + 256 * '2' + 256 * 256 * '3' + 256 * 256 * 256 * '4'
or :
'4' + 256 * '3' + 256 * 256 * '2' + 256 * 256 * 256 * '1'
<>13, Scope ( visibility ) Survival time

* Scope : It is aimed at the process of compiling and linking
* function , Global variables from definition ( The entire file is visible ) Globally visible , There is no local function
* Survival period ( Life cycle ): It is aimed at the execution process of the program
* Life cycle of local variables : Function called to start , Dies at the end of function execution , Free up storage space . store in .stack area
* Lifetime of global variable : Start when the program starts running , End at the end of program execution . store in .data area
*
Dynamic life cycle ( Heap space ): Identifiers are created and released by specific function calls or operations , If called malloc() Start by allocating storage space for variables ,free() End of freeing storage space . Stored in heap .heap
Compilation error :g_value Undetermined identifier
#include <stdio.h> void Test() { int a = g_value; } int g_value = 10; int main(
) { Test(); return 0; }
Misunderstanding : Mistakenly believe that the program is running while compiling ,g_value Exist in .data paragraph

<>14,C Language operator priority

The name or meaning of the priority operator is described in combination with the direction
1[] Array subscript array name [ Constant expression ] Left to right
() Parentheses ( expression ) Function name ( Formal parameter table )
. Member selection ( object ) object . member name
-> Member selection ( Pointer ) Object pointer -> member name
2- Minus operator - Expression right to left monocular operator
( type ) Cast type ( data type ) expression
++ increment operator ++ Variable name Variable name ++ unary operator
– decrement operator – Variable name Variable name – unary operator
* Value operator * Pointer variable unary operator
& address operator & Variable list operator
! Logical non operator ! Expression unary operator
~ Bitwise negation operator ~ Expression unary operator
sizeof length operator sizeof( expression )
3/ Division expression / Expression left to right binocular operator
* Multiplication expression * Expression binocular operator
% remainder ( Take mold ) Integer expression % Integer expression binocular operator
4+ Additive expression + Expression left to right binocular operator
- Subtraction expression - Expression binocular operator
5<< Shift left variable << Expression left to right binocular operator
>> Shift right variable >> Expression binocular operator
6> Greater than expression > Expression left to right binocular operator
>= Greater than or equal to expression >= Expression binocular operator
< Less than expression < Expression binocular operator
<= Less than or equal to expression <= Expression binocular operator
7== Equal expression == Expression left to right binocular operator
!= Not equal to expression != Expression binocular operator
8& Bitwise and expression & Expression left to right binocular operator
9^ Bitwise XOR expression ^ Expression left to right binocular operator
10| Bitwise or expression | Expression left to right binocular operator
11&& Logic and expression && Expression left to right binocular operator
12|| Logical or expression || Expression left to right binocular operator
13?: Conditional operator expression 1? expression 2: expression 3 Right to left trinocular operator
14= Assignment operator variable = Expression right to left
/= Assignment variable after division /= expression
*= Multiplicative assignment variable *= expression
%= Assign variable after module %= expression
+= Post addition assignment variable += expression
-= Subtracted assignment variable -= expression
<<= Left shift assignment variable <<= expression
>>= Assign variable after moving right >>= expression
&= Bitwise and post assignment variables &= expression
^= Assign variable after bitwise XOR ^= expression
|= Bitwise or post assignment variable |= expression
15, Comma operator expression , expression ,… Left to right
Error prone point :
int main() { int a = 1, b = 2; a *= b + 5; //+ Priority of is higher than *= therefore a = a * (b + 5) -->
a = 7 printf("%d\n", a); //7 }
<>15, Pointer storage —— Small end storage

Small end storage : The high-order number is stored in the high address , The low bits are stored in the low address ;

Both value storage and address storage follow the small end storage

<>16, Standard input file 0, Standard output file 1, Standard error output file 2

When a program starts running , These three files are opened by default ;

*
Standard input file stdin: The corresponding file descriptor is 0, The keyboard input is mapped into a standard input file through some mapping relationship ;stdin There is a row buffer in memory , When line breaks are encountered (’\n’) Will be entered into the buffer ;
*
Standard output file stdout: The corresponding file descriptor is 1, Map the screen output to a standard output file through some mapping relationship ;stdout There is a row buffer in memory , When line breaks are encountered (’\n’) Will be output to the screen ;
* Standard error file stderr: The corresponding file descriptor is 2, Is bufferless , It is output directly on the screen ;
Program case : Get the number of characters output from the keyboard

<>17, Macro sum typedef
#define PINT int* // Macro replacement , Regardless of type and size typedef int* TINT; // Type rename , Type and size identification int main
() { PING a, b; //int* a, b; TINT p, q; //int* p; int* q; }
<>18,extern keyword

extern Used before the declaration of a global variable or function , Used to illustrate “ This variable , Functions are defined elsewhere , To reference here ”;

Usage scenario : Different files under the same project

file fun.c
int g_max = 10; void fun() { g_max +=10; printf("%d\n", g_max); }
file main.c
#include <stdio.h> extern int g_max; extern void fun(); int main() { int a =
g_max; fun(); }
C++ Medium extern Other uses of ;

<>19,static Use of keywords

Memory function : This function contains static local variables ;

Static local variable : When a function is called for the first time , The local static variable in the function is initialized , When this function is called again , The static variable is not initialized , The value of the local variable after the last function execution is retained ( Scope invariant , Survival change )

*
be careful :C Language static local variables can only be initialized once with constants ;

​ C++ You can initialize once with constants and variables

Problem solving :

*
Can formal parameters be added static

answer : add , Compile passed , But this variable is a “ bad ” Storage class ;

So the parameters are not added static

*
How is the memory function called at the first initialization , Not called later ?

answer : In the compilation phase , The compiler stores the static local variables in the memory function in .data Segment and give the variable a record value val =
1, When the program executes a statement that defines a static local variable , Judge the recorded value first , If val == 1 Description first call , After execution val–; otherwise val == 0 , Skip this statement ;

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-adZpltRu-1635073955445)(C:\Users\ Little freak \AppData\Roaming\Typora\typora-user-images\image-20211024161744487.png)]

* be careful :static int a = 10;
Thread safety needs to be considered in multithreading , Multiple threads execute the statement at the same time , The value is one of val The value will be obtained at the same time , In this way, the statement may be executed multiple times . The problem of singleton mode needs to consider thread safety
static global : Static global variables can only be used in the current file ( Scope restricted , The survival time remains unchanged )

*
be careful 1: When global variable , Function plus static after , Scope is limited by this document , Other files are inaccessible ; Even if other documents add extern Keyword declarations are also unavailable

main.c file
#include <stdio.h> extern int g_max; extern void fun(); int main() { int a =
g_max; // Compilation error , Unresolved command g_max fun(); return 0; } fun.c file static int g_max = 10;
static void fun() { printf("%d\n", g_max); }
be careful 2: hope fun.c In file const int a = 10;
A constant is called by another file , Just before the variable definition, add extern, For the files used at the same time, the value of the variable should also be added extern statement

main.c file
#include <stdio.h> extern int g_max; int main() { int a = g_max; printf("%d\n",
a); return 0; }
fun.c file
extern const int g_max = 10; // Externally visible constant
​ //extern static int g_max = 10;
​ //extern External visibility and static Visible contradictions in this document

Static function :static The function word described can be in the current c Used in files ( Limited scope , The survival time remains unchanged )

<>20,4G Virtual space

<>21, The location where data is stored in memory
#include <stdio.h> int g_maxa = 10 int g_maxb; int g_maxc = 0; static int
g_maxd; // The default is 0 static int g_maxe = 0; static int g_maxf = 10; int main() { int
maxa= 10 int maxb; int maxc = 0; static int maxd; static int maxe = 0; static
int maxf = 10; }
<>22,const Modify defined variables and #define Difference between macro substitution ( see 5)

*
Different processing objects :const The modifier is the defined variable , Macro substitution defines constants

*
Different processing periods :const Modified variables are determined during compilation , Macro replacement is performed during the precompiling period ;

*
Space occupied and type :const Modified variables have size and type , Constants replaced by macros do not take up space , No type check

<>23, On macro function

Is a simple replacement
#include <stdio.h> #define SUM(x, y) x*y int main() { int a = 3, b = 4; int c =
SUM(a + 1, b + 2); //int c = a+1*b+2 printf("%d\n", c); return 0; } // Solution / #
define SUM(x, y) (x)*(y)
Hum ~ Ah, ah, ah ~ End la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la !!!!!!!!!!!!!!!!!!!!!!!!!!!

Technology
©2019-2020 Toolsou All rights reserved,
Android Using wechat in H5 Payment result refresh during payment shock !!C++ Can make a sound ! Basic operation of single linked list C Language explanation Java Implement an epidemic number management system 2021 year 11 World programming language ranking linux upper mysql Invalid command _linux lower mysql The command is useless Java project : Campus dormitory management system (java+jsp+javaweb+mysql+ajax) Wechat applet development project directory linux ubuntu Which version ,Ubuntu Which version is the best ?python Code painting cherry blossoms - How to use Python Draw a beautiful cherry blossom