Day 3: get to know keywords, constants and macros, pointers and structures

Today is the third day of relearning the C language. I think that the blog is not only used to share my daily learning results, but also can be written about my planning and thinking every day. On the third day, I went through the basic parts of the C language, which is a simple review. Next, I will start to learn from the point to point. I feel that the current progress is acceptable, Strive to complete the advanced before the end of the winter vacation. The current state is very confused about my future, but the planning and direction for the near future are accurate. I hope quantitative change can cause qualitative change. Come on!

keyword

Keywords cannot be variables

auto  break   case  char  const   continue  default  do   double else  enum   extern float  for   goto  if   int   long  register    return   short  signed sizeof   static struct  switch  typedef union  unsigned   void  volatile  while

Auto is automatic - every local variable is modified by auto and omitted

break loop sequence jump out

const modifier constant

enum enumeration

extern is used to declare external symbols

Register register keyword

Signed signed

sizeof size

Static static (afterschool)

void none, empty

volatile (not required)

define/include preprocessing instruction, not keyword

typedef type redefinition

typedef unsigned int u_int;

int main()

{

unsigned int num = 100;

u_int num2 = 100;

return 0;

}

Static - static

1. Modify local variables: it essentially changes the storage type of variables, so it changes the life cycle

void test()

{

static int a = 1; //Out of scope a not destroyed

a++;

printf("%d", a);

}

int main()

{

int i = 0;

while (i < 10)

{

test();

i++;

}

return 0;

}

2. Modify global variables

Global variables can be used in the whole project, and only need to be declared by extern

static modifies the global variable, so that the global variable can only be used in its own source file (. c)

Global variables can be used in other source files because they have external link attributes, but after being modified by static, they become internal link attributes, and other source files cannot be linked

3. Modification function

Similar to global variables

Constants and macros

define is a preprocessing instruction

//Define identifier constant #define MAX 1000

//define macro
#define ADD(x, y) ((x)+(y)) 
#include<stdio.h>

int main() 
{
    int sum = ADD(2, 3);
    printf("sum = %d\n", sum);
    sum = 10*ADD(2, 3);
    printf("sum = %d\n", sum);
    return 0; 
}

Pointer

32 bits - 32 address lines - physical lines - power on - 1 / 0

A memory unit is a byte

Fetch address &a - fetches the address of the first byte

printf("%p\n"&a); % P is dedicated to printing addresses

int * pa = &a; / / PA is called pointer variable, which is specially used to store addresses

/ / * indicates that pa is a pointer variable, and int indicates that the object pointed to by pa is of type int

#include<stdio.h>

int main()

{

    int num = 10;

    int *p = &num 
    *p = 20; //*The dereference operation * pa is to find num through the address in pa

    return 0;

}

#include / / the size of the pointer variable depends on the size of the address

//The address under 32-bit platform is 32 bits (i.e. 4 bytes)

//The address under 64 bit platform is 64 bit (i.e. 8 bytes)

int main()

{

   printf("%d\n", sizeof(char *));

   printf("%d\n", sizeof(short *));

   printf("%d\n", sizeof(int *));

   printf("%d\n", sizeof(double *));

   return 0;

}

structural morphology

 struct Stu 
 {
    char name[20];//name
    int age;      //Age
    double score //achievement
 };
 //Print structure information
 struct Stu s = {"Zhang San", 20, "85.5"};

 //Accessing operators for structure members
 printf("name = %s age = %d score = %lf\n", s.name, s.age, s.score);

 The following is the printing method for the pointer
 //->Operator
 struct Stu *ps = &s;
 printf("%s %d %lf\n",(*ps).name,(*ps).age,(*ps).score)
 printf(" %s %d %lf\n", ps->name, ps->age, ps->score);

Keywords: C

Added by minidak03 on Thu, 03 Feb 2022 02:13:35 +0200