First knowledge of C language (first level of C language)

Posted by pococool on Mon, 27 Dec 2021 04:57:46 +0100

catalogue

12. #difine defines constants and macros

13. Pointer

13.1 memory (address)

13.2. How to get the address

13.3 storage of addresses

13.4 other types of pointers and pointer sizes

14. Structure

12. #difine defines constants and macros

#include<stdio.h>

//#Identifier constant defined by define
//NUM -- this string of characters is equivalent to a constant -- 2000
#define NUM 2000

//#Define defined macro -- use #define defined macro to add two numbers
#define Add(x, y) ((x)+(y))

//Use function to add two numbers
int ADD(int x, int y)
{
	return (x + y);
}

int main()
{
	int a = 10;
	int b = 20;
	int c = NUM;
	printf("%d\n", c);     //Print value 2000
	int sum1 = ADD(a, b);
	printf("%d\n", sum1);     //Print value 30
	int sum2 = Add(a, b);
	printf("%d\n", sum2);     //The print value is 30
	return 0;
}

The brackets in #define Add(x,y) ((x)+(y)) should not be omitted

In case of unexpected results:

For example, the following code

#include<stdio.h>

#define Add(x, y) ((x)+(y))
#define ADD(x, y)  x+y

int main()
{
	int a = 10;
	int b = 20;
	int sum1 = 2 * Add(a, b);
    int sum2 = 2 * ADD(a, b);
	printf("%d\n", sum1);     //The print value is 60
    printf("%d\n", sum2);     //The print value is 40
    return 0;
}

13. Pointer

13.1 memory (address)

(1) Memory is a particularly important memory on the computer. The operation of programs in the computer is carried out in memory.

(2) 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.

(3) In order to effectively access each unit of memory, the memory unit is numbered. These numbers are called the address of the memory unit.

 

(4) Supplement:

The number preceded by 0x - represents hexadecimal number - hexadecimal number of 2 digits = binary number of 8 digits (for example, hexadecimal - FF and binary - 11111111 represent the same number)

On the 32-bit platform: there are 32 address lines. If each address line can give 0 or 1, it can give the 33rd power of 2 - 1 numbered address

33rd power byte of 2 = 4294967296byte = 4194304kb = 4096mb = 4GB

Therefore, on the 32-bit platform (32 address lines): the maximum memory is 4GB.

If you want to increase memory, you need to add address lines. You can use a 64 bit platform.

13.2. How to get the address

Using the & (address) operator

#include<stdio.h>
int main()
{
	int num = 0;    //&Num -- fetch the address of num
//num is an integer, an integer is 4 bytes, and the address of the first byte is taken out
	printf("%p\n", &num);   //%p -- take address -- print in the form of address
	return 0;
}

Supplement:

%d -- print in integer form

%c -- print in character form

%s -- print as string

%f -- print in the form of single precision floating point type

%lf -- print as double precision floating point

%p -- print in the form of address

%lld -- print as a longer integer

 

13.3 storage of addresses

Pointer variables need to be defined for address storage

Take integer pointers as an example:

#include<stdio.h>
int main()
{
	int num = 0;
	int* p;   //Defines a pointer variable, (int* p), where * indicates that p is a pointer variable, and int indicates that the type of p pointer variable is an integer
	p = &num;   //p is a pointer variable used to store the address -- here is the address of num.
	*p = 10;   //*Is a dereference operator. You can find the content stored in the address according to the address
	return 0;
}

Supplement:

'*' in (int* p) and '*' in * p = 10 have different meanings

'*' in (int* p) indicates that p is a pointer variable

*'*' in p = 10 indicates dereference operation

13.4 other types of pointers and pointer sizes

char* p  //Character pointer
int* p  //Integer pointer
short* p  //Short integer pointer
long long* p  //Long pointer
float* p  //Floating point pointer
double* p  //Double precision floating point pointer

Of course, there are array pointers and a series of pointers (later)

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

The size of the pointer is 4 bytes or 8 bytes (depending on whether you use a 32-bit platform (4 bytes) or a 64 bit platform (8 bytes))

The pointer variable is used to store the address. The address is the number - the size of the number depends on the platform - so the pointer size depends on the platform size

14. Structure

Structure is a particularly important knowledge point in C language. Structure can describe complex types of variables.

For example, the description book bag contains: Book Name + price + book number.

Only structure can be used here

#include<stdio.h>

struct Book  //Custom book type
{
	char name[20];  //title
	float price;    //Price
	char id[20];    //Book number
};

void Print(struct Book* pb)
{
	printf("%f\n", pb->price);  //(- >) operator - structure address - > member name
	printf("%s\n", (*pb).id);   //(*pb).id and Pb - > ID mean the same thing
}

int main()
{
	struct Book b = { "C Language programming", 45.5f, "C00110101" };
	Print(&b);   //Print function
	printf("%s\n", b.name);  //(.) operator - structure variable member name
	return 0;
}

The printed content is:

 

Topics: C#