当前位置:网站首页>"First knowledge of C language" (Part 3)

"First knowledge of C language" (Part 3)

2022-06-23 01:42:00 'Dream_

If you will It's better to start than to give up .     Roman.

May we all have our own goals and are making unremitting efforts for them .

Summary of recent studies It can be regarded as a kind of explanation for those who are willing to re-establish themselves a farewell 2021.

Twelve 、 The pointer

  One ) Memory

 1、 To better manage memory , Divide the memory into small memory units , The size of each memory unit is 1byte

  •   Don't use bit As the memory unit size is too small , Too much memory , As simple as char The type is 1byte, This will waste 8bit, namely 8 Memory units
  • Each memory unit is numbered , The number is the memory unit address

  2、 How to number ?

    Number by digits

     ① Such as :32 Bit machines have (2^32) Sort of arrangement , That is, the maximum number of numbers is (2^32)

    therefore , There is maximum space (2^32)byte, Divide by 1024 You can get the size of its memory space 4GB(1024 Is the conversion relationship between two adjacent units ); In this way, we can find 64 Bit machine memory

   ② Again , And then to 32 For example, a bit machine , Each of them has 0/1 Two possible , That's why (2^32) Sort of arrangement , That is, the maximum number of numbers

   ③32 position , That is to say 32 Root address line / cable

    Address line : Similar to electric wire , After power on, the wire number can be converted into a digital signal 1/0( High and low level )

    Its scope :00000000000000000000000000000000(32 position )

                ......

                 11111111111111111111111111111111(32 position )

    The length of binary representation is too long , So it is expressed in hexadecimal instead --8 Hexadecimal digits can be used to represent 32 Bit binary

    Such as :0x00000000   0xFFFFFF    ( among 0x Is a hexadecimal flag prefix )

  •   The value converted to decimal is the address number

   3、 When a variable occupies more than one byte , Just focus on the starting address of the variable

     %p It's a printed address , Such as :printf("%p\n",&a) The final result is a The first address

  Two ) Pointer to the variable

  1、 Memory cells have numbers , This The number is the address , Call this address a pointer !

  2、 int  * pa=&a;

  use pa To hold the a The address of , The type is int  *   (int* Because a The type is int, When a When the type of int It also changes )

  ① take pa It's called a pointer variable

  ②* The meaning of :pa Is a pointer variable

  ③int Tell us :pa The object is int Type of

  3、*pa   pa It's a pointer variable ,* Dereference operator

  It means : adopt pa Find the address stored in pa Point to : Such as *pa ='a' ; Then represent pa The content pointed to is modified to a

  4、 The size of the pointer variable : The size is fixed to 4byte!(32 Bit machine )

    Because the pointer variable is the address , Address 32 Bitwise is 32bite=4byte; Of course , If 64 Bit machine , be 64bit=8byte size !

  3、 ... and ) Structure

  struct yes C Language custom data type Not a built-in data type

  1、C Languages want to describe complex types     Such as : Describe the students : name 、 Age 、 Gender 、 Student number, etc

   1)// Type of structure -- Equivalent to drawing

     struct  Stu

     {

       // name -- character string   Use an array to represent a string !

      char name[20];

     // Age -- Integers

     int age;

    // Gender -- character string

    char sex[5];

    // Student number -- character string

    char id[20];

   };

Written in main Outside the function !

   

 2) stay main Call in function

  // Structural variable -- Equivalent to a house , Real storage

  struct Stu s1={" Zhang San ",18," Woman ","20203101218"};   // initialization

  // Print directly

  printf("%s %d %s %s\n",s1.name,s1.age,s1.sex,s1.id);

  // Function print

  In the main function :Print(&s1);

  Write the function outside the main function :

  void Print(struct Stu * ps)

 {

 //printf("%s %d %s %s\n",((*ps).name,(*ps).age,(*ps).sex,(*ps).id);

  printf("%s %d %s %s\n",ps->name,ps->age,ps->sex,ps->id);

  • Supplementary input

   // Input

   scanf("%s %d %s %s\n,s1.name,&(s1.age),s1.sex,s1.id);

   // Be careful : Now it's Variable .    Then notice that the array name is the address , No need to add &, however int To add &

 }

 3) Be careful : After the pointer variable is dereferenced .

                ( Address ) Pointer to the variable ->

 4) You can give The structure type redefines the name , Convenient for subsequent use

    Such as : typedef struct Stu

          {

         }stu;

      When used later, you can  typedef struct Stu Change all to stu
  2、struct Is the structure keyword

  3、 Structure pointer -> Structural members

        Structural variable . Structural members

    ( Result member access operator   .  ->)

13、 ... and 、 compiler

 1、GCC

 2、MSVC: Such as VS2019 Integrated development environment

    Tools -- Options : You can change the font color, etc

    debugging :f10

fourteen 、C Language program

  Write code ( With VS2019 For example ):

 1) compiler

 2) The new project / Empty item :

  ① Project name should not be in Chinese characters

 ② Do not add special characters

 ③ No spaces

 ④ Pay attention to changing the storage location ( You can create new folders )

 ⑤ View -- Solution explorer

 3).c  Source file       .h The header file          .cpp(C++ Source file )

 4) Code

  ①VS2019 First write in the source file : #define _CRT_SECURE_NO_WARNINGS 1

  •    VS Think C Language provides scanf There are security issues , use scanf_s Can solve , But it's not universal , So it is best to add the above statement to the source file , Keep using scanf
  • VS All the new source files in are copies newc++file.cpp Of documents , So find the file and copy it to the desktop , Enter the above and then replace with the original path to keep it permanently , Once opened, it exists

 ② The main function  

     int main()

    {     

      return 0;

  }

  •  int integer And back return Of 0 Corresponding
  • The main function has only one
  • Under the same project Even if multiple source files exist , But there can only be one main function !
  • The main function is the program entry !

 ③printf Print function : You need... In the function #include<stdio.h>  

   std i o : Standard I / O functions

 ④scanf Input function

  •   Array input none & Just enter the array name ( The array name is the address )
  • scanf Don't care if the input string length overflows , Therefore, it is not safe

  5) compile link function :ctrl+f5  or  ctrl+fn+f5

15、 ... and 、 Computer unit

 bit   byte   kb   mb   gb   tb   pb

 1 byte=8 bit    With the exception of , The conversion relationship between two adjacent units is 1024

sixteen 、 Supplementary content

  Multi group input : while(scanf()!=EOF) {  }

  •  scanf The call to the function failed , return EOF
  • EOF End of file   Its essence is -1

原网站

版权声明
本文为['Dream_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514206983.html