First knowledge of C language 3

Keywords: C Back-end

  Due to the recent delay of professional courses and various things, I'm sorry for the late update, but it came today. Here is the last section of getting to know the C plate. The subsequent updates will be more specific and detailed ūüėč

1. Common keywords

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

  Here are typedef and static

 typedef:

static:

 

2.define constants and macros

#Identifier constant defined by define

#include <stdio.h>
#define MAX 100
#define ST  "haha"
int main()
{
	printf("%d\n", MAX);
	printf("%s\n", ST);
	return 0;
}

 

 

 # define macro     Similar to function

Function mode

#include <stdio.h>
int MAX(int x, int y)
{
	if (x > y)
		return x;
	else
		return y;
}

int main()
{

	int a = 10;
	int b = 20;
	int max = MAX(a, b);
	printf("max=%d\n",max);
	return 0;
}

 

Macro mode

#include <stdio.h>
#Define max (x, y) (x > y? X: y) / / macro definition
int main()
{
	int a = 10;
	int b = 20;
	int max = MAX(a,b);
	printf("max=%d\n",max);
	return 0;
}

 

 

3. Pointer  

Memory: memory is a particularly important memory on the computer. The operation of programs in the computer is carried out in memory. Therefore, in order to use memory effectively, the memory is divided into small memory units, and the size of each memory unit is 1 byte. In order to effectively access each unit of memory, the memory unit is numbered. These numbers are called the address of the memory unit.  

Fetch variable address:

#include <stdio.h>
int main()
{
	int num = 10;
	&num;//Fetch the address of num
	                       //Here, there are 4 bytes of num, each byte has an address, and the address of the first byte (smaller address) is taken out
	printf("%p\n", &num);//Print address,% p is printed as an address
	return 0;
}

 

For address storage, pointer variables need to be defined

int num = 10;
int* p;   //p is an integer pointer variable
p = &num;

  Use the following:

#include <stdio.h>
int main()
{
 int num = 10;
 int *p = &num;
 *p = 20;
    return 0;
}

p is a pointer variable used to store the address. int * indicates that q is a pointer variable (equivalent to the defined type), * is a dereference operator. In * p, it means to find num through p storing the address, which is equivalent to * p=num* If p=20, modify the value of num.

Extensions: other types

#include <stdio.h>
int main()
{
 char ch = 'w';
 char* pc = &ch;
 *pc = 'q';
 printf("%c\n", ch);
    return 0;
}

 

Size of pointer variable:

#include <stdio.h>
int main()
{
    printf("%d\n", sizeof(char *));
    printf("%d\n", sizeof(short *));
    printf("%d\n", sizeof(int *));
    printf("%d\n", sizeof(double *));
    return 0;
}

  If the platform is 32-bit:

  If the platform is 64 bit:

  Conclusion: the pointer size is 4 bytes on 32-bit platform and 8 bytes on 64 bit platform.

 

4. Structure

Structure is a complex type: it allows data members of different data types to be grouped together

#include <stdio.h>
#include <string.h>
struct Book                           //Create structure variable
{
	char name[20];//C language programming
	short price;//55
};

int main()
{
	struct Book b1 = {"C Language programming",55};    //initialization
	 printf("%s\n",b1.name);
	 printf("%d\n",b1.price);
	return 0;
}

If address:

	struct Book b1 = { "C Language programming", 55 };
	struct Book* pb = &b1;

When printing, it can also be expressed as follows:

	printf("%s\n",(*pb).name);
	printf("%d\n",(*pb).price);

. operators accessed for struct members  

Structure variables. Structure members

There is also the - > operator:     Structure pointer - > member

	printf("%s\n",pb->name);
	printf("%d\n",pb->price);

 

This is the end of getting to know plate C for the first time ūüėÉÔľĆ You are welcome to correct the problems!

Posted by reli4nt on Mon, 25 Oct 2021 19:21:13 -0700