C language programming notes

The journey of a thousand miles begins with a single step, and the nine-story pagoda starts from the base of the soil. C is not solid enough, so I restarted the practice. Some notes that need to be paid attention to during the process

1. The original code, inverse code and complement code of positive numbers are the same!

2. Logic operation
Rule 1: As long as one of the operands is a floating-point type, the expression is a floating-point type, and the operand is an integer, and the expression is also an integer.
For example:

float a=0,c=0;
a=5/2;
printf("%f\n",a);

prints 2
instead of 2.5

Solution One of the operands is changed to a floating-point type, and the output expression is a floating-point type.
Rule 2: The two operands of the % remainder operator must be integers, and the sign of the result of the expression is determined by the dividend.
Example
insert image description here

Detailed rule 3: self-increment and self-decrement operator
a++;
read first and then write,
first read a and then perform arithmetic operations
, namely:
printf(“d%\n”,a);
a=a+1;

++a;
write first and then read
First perform arithmetic operation on a and then read
:
a=a+1;
printf(“d%\n”,a);

Rule 4 Assignment operator
insert image description here
Rule 5:
The result of sizeof (expression) is the number of bytes occupied by the data type of the expression
Rule 6: Mixed operations of different types of data, the computer will automatically convert to high-precision data to ensure the accuracy of the operation Type
For example:
char must be converted to int
float must be converted to double
insert image description here
3. Algorithm
Rule 1: Input parameter
scanf("%d%d", &a, &b);
Rule 2: Find odd and even numbers
if (a%2! 0)
{}
Rule 3: Commonly used statements
if (expression) {}
if (expression) {} else (expression) {}
if (expression) {} else if (expression) else (expression) {}
switch(expression)
{ case constant expression 1: statement 1; break;
case constant expression 1: statement 1; break;
default: statement
}

Interpretation:
1. If the if expression is true, execute the if content.
2. If the if expression is true, execute the if content. If it is false, execute the else content.
3. If the if expression is true, execute the if content. If it is false, the else if
expression is If the content of the else if is true, execute
the
content of the else if it
is false. Value) break statement (break out of the switch) The switch expression cannot be a floating-point type, it must be a fixed value } 4. One-dimensional array rule 1: The length of the array must be an integer constant expression, which can be precompiled in the macro definition formula, refer to the macro definition in the array Rule 2: The unassigned array, the content is unknown Example: int arr[5]; arr[0]=?;arr[1]=? ... Rule 3: Partial assignment, the remaining unassigned part is 0 int arr[5={0};]; arr[0]=0;arr[1]=0... Rule 4: Use sizeof() to get the length Rule 5 : Get array length (total number of bytes/sizeof(type)) Example: int arr[5]={0}; printf("length: %d\n", sizeof(arr)/sizeof(arr[0]) )















5. Two-dimensional
array Rule 1: A two-dimensional array occupies multiple continuous memory spaces, which is equivalent to the continuity of a one-dimensional array
Example:
insert image description here
Rule 2: Total bytes = sizeof (array type) * number of rows, number of columns
Rule 3: Calculation The formula for the position of the element in the array
Example a[ 1 ][ 2 ] is the 1st in the array
3+2=5th
6. One-dimensional array

7. Two-dimensional array

8. Structure
Details 1:
Example
struct student
{ int num; int c_score ; int ps_score; float avg ; } ; Cannot assign initial value Method of assigning initial value 1.struct student Tom={10,90,98,94.5}; 2.Tom.num=10; Detail 2: While declaring the type, define the variable Example 1 struct student { int num ; int c_score; int ps_score; float avg; }; //struct student //type name //struct student Tom; //Tom variable //struct student Tom = {10}; //struct initial value, similar to an array Assignment //After defining the structure variable, //You can use Tom.num = 10; to assign values ​​to the structure members Example 2


























struct student
{ int num; int c_score; int ps_score; float avg; }Tom;//Tom is a structure variable (variables are defined while declaring, equivalent to struct student Tom;) Example 3 struct student { int num; int c_score; int ps_score; float avg; }Tom; Jary;//Tom and Jary are structure variables (variables are defined at the same time as declaration, which is equivalent to struct student Tom;) Rule 3: Variables occupy memory space Rule 4: Structure is omitted Name, directly define the structure variable struct //student (this method can only be defined once, and then the structure variable cannot be defined again) { int num; int c_score; int ps_score; float avg; }Tom; Jary; //Tom, Jary is a structure variable Rule 5: Structure array assignment struct student





























{ int num; int c_score[2]; int ps_score; }Tom; Jary; //Tom and Jary are structure variables Assignment: struct student Tom = {10, {20,30,40}, 50};// Prevent the array from missing struct student Jary = {10, 20, 30, 40, 50};






8. Typedef
gives the existing type an alias
typedef signed char int8;
//signed char a is equivalent to int8 a;
detail 2:
use of typedef structure
Example 1
typedef struct student
{ int num; int c_score[2]; int ps_score ; }STU;//After adding typedef, the STU here is no longer a structure variable //but an alias of the structure struct student STU Tom={10, {20,30,40}, 50}//equivalent to struct student Tom





Rule 3
& is the address character//transmit to the target address

9. Structure array
Definition method 1
struct student
{ int num; int c_score[]; int ps_score; };//Definition structure struct student class1[5];




Definition method 2
struct student
{ int num; int c_score[]; int ps_score; }; class1[5]; //class1 is the structure array name/represents a structure with 6 struct student types



Initialization method
struct student class1[2] = { {10,{20,30,40},50},{10,{20,30,40},50},{10,{20,30,40},50 }};//Initialize the initial values ​​of the members of the 3 structures

Structural array use
Example 1
requires to find the highest score among N students and output the student information
typedef struct student
{ int num; int score; }STU; //STU is the alias of the structure STU class1[n]= { { {10,90},{20,70},{30,95}}; int i =0; int max=0; for(i=1;i<n;i++) { if(class1[i]. score > class1[max].score) max=i;//filter the highest score } print("The student with the highest score:\n") print("Serial number: %d\n", class1[max].num); print("serial number: %d\n", class1[max].score);












time:2021/11/24

9. Scope and variables
Rule 1:
File scope (global variable): Covers the file from beginning to end
Function scope (local variable): Parameters and function body belong to function scope, return value function name belongs to file scope
Rule 2:
Conforms to the statement block: {}, which belongs to the scope of the statement block
Rule 3:
1. Different scopes will not conflict
2. The same scope is close to each other
Rule 4:
extern extended scope: file scope identifiers can extend their scope through it

10. Storage internal
rules 1:
Stack variables: auto modification (default omitted) -> 99% of local variables
static modified local variables will directly open up memory blocks at compile time -> 1% of local variables
destroy memory blocks: function execution After the completion, the memory will be returned.
Rule 2;
variable declaration -> open up a memory block
beyond the scope of the scope -> in fact, the memory space of the local variable is destroyed after the scope is executed
Rule 3:
Global variables (static variables): use static to declare
memory Development time: compile time
Memory destruction time: at the end of the main function
Rule 4
formal parameters cannot be declared static, they can only be used as local variables modified by auto
Rule 5
Variables are not assigned a default value of 0
Rule 6
Global identifiers (macros/variables/functions) use static Modification, does not indicate the global area, but indicates that the identifier can only be extended and used in this file

Expansion: Identifier naming rules:
macro name -> all uppercase,
variable name -> single word is lowercase, the first word of multiple words is lowercase, and the first letter of each subsequent word is capitalized
Function name -> single word is uppercase, multiple words Capitalize the first letter of each word

11. Pointer
Rules 1
Pointer is the address
& address symbol (printf("%p\n",&a);->get the address position of a in memory),
where the address of a is a constant, and the address of a variable is a constant and cannot be modified and transformed

12. Pointer variable
Rule 1

  • *a // * pointer operator

time: 2021/11/25
Reference from
https://www.bilibili.com/video/BV1iz4y1S7NK?p=11

Guess you like

Origin blog.csdn.net/weixin_49048045/article/details/121235076
Recommended