Initial C language (fifth lesson)

Table of contents

1. Keyword introduction

    1. General category of keywords:

    2. Keyword typedef introduction

    3. Keyword static

    1. Constants defined by #define

    2. #define definition macro

3. Pointer

    1. Introduce memory and definition

    2. Examples:

    3. Memory for pointer variables 

    4. Common cases

Four. Structure

    1. Definition

    2. How to define

1. Keyword introduction

1. General category of keywords:

auto local variable (automatic storage)

break Unconditionally exit the innermost loop of the program

Select items in case switch statement

char Single-byte integer data

const defines a constant value that cannot be changed

continue interrupts this loop and goes to the next loop

Default selection in default switch statement

do is used to form a do.....while loop statement

double defines double-precision floating-point data

else constitutes if.....else selection program structure

enum enumeration

extern declares global variables in other program modules

float defines single-precision floating-point data

for constitutes a for loop statement

goto constitutes a goto transfer structure

if constitutes an if...else selection structure

int Basic integer data

long Long integer data

register variables stored inside the CPU

return is used to return the return value of the function

short short integer data

signed signed number

sizeof calculates the number of bytes occupied by an expression or data type

static defines a static variable

struct defines structure type data

switch constitutes a switch selection structure

typedef redefines a data type

union union type data

unsigned defines unsigned data

void defines untyped data

volatile The variable can be implicitly changed during the execution of the program

while is used to form a do...while or while loop structure

main main function

printf print

system execution

Notepad notepad();

Process management tasklist();

await getchar();

2. Keyword typedef introduction

(1) Definition: Redefine data types (replace complex and extremely long data types with short names, mostly used in structures)

(2) Simple example:

unsigned int a=0;//unsigned int为无符号整形,是一个整体
int b=0;

We can see that it is a waste of time to type unsigned int every time, so we can abbreviate it

typedef unsigned int unit;//缩写为unit
typedef int I
int main()
{
     //unsigned int a=0;
     unit a=0;//与上面的效果一样

     //int b=0;
     I b=0;

     return 0;
}

Obviously, using abbreviations can save time and cost

3. Keyword static

(1) Definition: used to modify variables and functions

(2) Modified local variables: After local variables are modified by static, they are called static local variables.

Example: We want to call the function to print 1 2 3; if the code is written as follows, the printed result is 1 1 1

#include<stdio.h>
int test()
{
  int j=0;
  j++;
  printf("%d",j);//打印了三次
}
int main()
{
   int i=0;
   while(i<3)//循环三次
   {
      test();//调用了三次函数
      i=i+1;
   }
  return 0;
}

Why is the result of the program running 1 1 1? First, every time the test function is called, then enter the test function, and then create the local variable j, then return to the while loop and call after printing, and return to the while loop again, because the local variable j has gone out of scope and is blocked Destroyed , and re-entering the test function needs to be recreated . At this time, it starts from 0 again, so the result of each print is 1.

In order to achieve the purpose, we can use the keyword static

#include<stdio.h>
int test()
{
  static int j=0;//修饰后使其出了作用域也不会被销毁
  j++;
  printf("%d",j);//打印了三次
}
int main()
{
   int i=0;
   while(i<3)//循环三次
   {
      test();//调用了三次函数
      i=i+1;
   }
  return 0;
}

After the local variable is modified by static, it will not be destroyed even if it goes out of scope, so the variable j is retained. After entering the function for the first time, the value of j becomes 1. When entering again, because the variable j still exists, it will not be created again, so the printed result will be 1 2 3

(3) Modify global variables

       We now have two files (.c files): a file and b file.

The global variable originally created on the a file can be used after being declared in the b file, but it cannot be used after being modified by static.

(4) Modification function

     We now have two files (.c files): a file and b file.

The function originally created on the a file can be used if it is declared in the b file, but it cannot be used after being modified by static.


2. #define defines constants and macros

1. Constants defined by #define

(1) How to define:

#define M 100//类似头文件
#define ch 'A'//定义了一个字符常量

(2) How to use:

#include<stdio.h>
#define M 100
#define ch 'A'
int main()
{
   //直接打印
   printf("%d",M);
   printf("%c",ch);
   //定义数组
   int a[M]={0};
   return 0;
}

The constants defined in this way are no different from ordinary constants. It can be understood as: give 100 a new name, and give the character A a new name. (The advantage of this definition is that you need to change the value of the constant later, just modify the new name, such as: M, ch)

2. #define definition macro

(1) Summary: macros can have parameters, macros are a replacement

(2) Replace some simple functions:

#include<stdio.h>
int Add(int x,int y)
{
   int z=0;
   return z=x+y;
}
int main()
{
  int a=3;
  int b=4;
  c=Add(a,b);
  printf("%d\n",c);
  return 0;

}

With a macro you can write:

#include<stdio.h>
#define Add(x,y)(x+y)
int main()
{
  int a=3;
  int b=4;
  c=Add(a,b);
  printf("%d\n",c);
  return 0;

}

3. Pointer

1. Introduce memory and definition

    Computer memory is divided into units, and the unit of each unit is a byte. Each cell has a number which is called the address of that memory cell . In C language, this address is called a pointer .

    Let us illustrate with an example: think of the memory as a hotel , each unit as a room , and the number as the room number of the room , the room number can also be called the address , and we can find the room through the room number (address). Things can be stored in each room, which is called data in C language .

    For example, if we create an integer variable (int) with a memory size of four bytes, we need to find four consecutive units to store the integer variable . At this time, we need to treat the four units as a whole. To find this ensemble, just find the number of the first unit in the ensemble

     Doubtful: Where did the address come from?

2. Examples:
#include<stdio.h>
int main()
{
  //&:取地址符号  *解引用操作符
  int a=5;
  int* p=&a;
  //&a是取出变量a的首地址,然后存放到变量p中。
  //*说明p是指针变量。地址==指针==编号
  //用来存放地址的变量称为指针变量
  //int是在说明p指向的是int类型的变量(int类型的指针变量)
  //*p 通过地址找到地址所指向的对象

  return 0;
}
variable stored content the address of the variable
a 5 0x00004
*p 0x00004 0x00012

If another variable is used to store the address of the pointer variable p, this variable is called a secondary pointer, also called a pointer of a pointer, which is currently unnecessary to understand.

When we need to modify the value stored in variable a, we can do this

#include<stdio.h>
int main()
{
 
  int a=5;
  int* p=&a;
  *p=520;//通过地址找到地址所指向的对象,所以*p==a

  return 0;
}
3. Memory for pointer variables 

   Because the pointer is used to store the address, the memory required by the pointer variable is 4 bytes (32 bits); 8 bytes (64 bits)

 

4. Common cases

  Topic: Use a function to swap the values ​​of two numbers

typo;

#include<stdio.h>
void swap(int x,int y)
{
  int z=0;
  z=x;
  x=y;
  y=z;
  return;
}
int main()
{
   int a=10;
   int b=20;
   printf("%d %d",a,b);//交换前
   swap(a,b);
   printf("%d %d",a,b);//交换后
   return 0;
}

We can see that the values ​​of a and b are passed to the formal parameters x and y, the question arises, will the change of the formal parameter value affect the value of the actual parameter (a and b), the answer is not, because When passing by value, the formal parameter is a copy of the actual parameter . Changes to the copy will not affect the original. So the result of both prints is 10 20.

Then you need to use pointers, find the object through the address, and then change it fundamentally.

Correct spelling:

#include<stdio.h>
void test(int* x,int* y)
{
  int temp=0;//两个数交换必须借助第三方
  temp=*x;
  *x=*y;
  *y=*x;
}
int main()
{
   int a=10;
   int b=20;
   printf("%d %d",a,b);//交换前
   test(&a,&b);//将a和b的地址传递给指针x和y
   printf("%d %d",a,b);//交换后
   return 0;
}

The underlying logic is to change the actual parameter, that is, to change the address.

Four. Structure

1. Definition

  A structure is also a type of data type. Like int is one, but it can only describe one type. If the age is described, it can be described by int, and the height can be described by double, but how should a student be described? So we need to use structure types to describe some complex objects.

2. How to define

(1) Define the type

struct Stu//这个是一个类型,跟int一样,用来创建变量
{
  char name[20];
  int age;
};

(2) Create variables (initialization)

#include<stdio.h>
struct Stu//这个是一个类型,跟int一样,用来创建变量
{
  char name[20];
  int age;
};
int main()
{
  struct Stu s1;
  struct Stu s2;//创建了两个变量s1和s2
  //赋值
  struct Stu s1={'苏芸',20};
  struct Stu s2={'贾语',19};//分别对名字和年龄进行赋值



}

(3) Use of structure

use variables directly

#include<stdio.h>
struct Stu//这个是一个类型,跟int一样,用来创建变量
{
  char name[20];
  int age;
};
int main()
{
  struct Stu s1;
  struct Stu s2;//创建了两个变量s1和s2
  //赋值
  struct Stu s1={'苏芸',20};
  struct Stu s2={'贾语',19};//分别对名字和年龄进行赋值
   
  printf("%s %d\n",s1.name,s1.age);//通过变量找到结构体内部的成员,中间用点(.)
  printf("%s %d\n",s2.name,s2.age);



return 0;
}

Use address:

#include<stdio.h>
struct Stu//这个是一个类型,跟int一样,用来创建变量
{
  char name[20];
  int age;
};
int main()
{
  struct Stu s1;
  struct Stu s2;//创建了两个变量s1和s2
  //赋值
  struct Stu s1={'苏芸',20};
  struct Stu s2={'贾语',19};//分别对名字和年龄进行赋值
  struct Stu* p=&s1;
  printf("%s %d",p->name,p->age);

return 0;
}

Summary: Structure variable. Structure member Structure pointer -> Structure member

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131735351