First meet C language, C language learning 04

 # preface:

The codeword is not easy. Thank you for your likes, comments and collections. If you think it's OK, I hope you can pay attention to it. Thank you for watching. 🌹💖
ヾ (≥ ▽≤ *) o (refueling together)

Keyword typedef

typedef: as the name suggests, type definition is a type definition, which can be understood here as renaming a type.
For example:

int main()
{
	//Rename unsigned char to uch, so uch is also a type name
	typedef unsigned char uch;

		//Look at num1 and num2. The types of these two variables are actually the same
		unsigned char ch1 = 'a';
		uch ch2 = 'b';
	return 0;
}

Because some type names are too long, you can rename this type through typedef to simplify the workload.

Keyword static

In C language:
static is used to modify variables and functions
(1) Modify local variables - called static global variables
(2) Decorated global variables - called static global variables
(3) Decorating functions - called static functions

Modify local variables

contrast
Code 1:

void test()
{
	int j = 0;
	j++;
	printf("%d ", j);
}
int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		test();
	}
	return 0;
}

Code 2:

void test()
{
	static int j = 0;
	j++;
	printf("%d ", j);
}
int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		test();
	}
	return 0;
}

Operation results
Code 1:

Code 2:

Why?

(1) When static modifies a local variable, the local variable is out of scope and will not be destroyed. In essence, when static modifies a local variable, it changes the storage location of the variable (from stack area to static area)
(2) It affects the life cycle of a variable and makes its life cycle longer, just like the life cycle of a program.

Modify global variables

contrast
Code 1:

//In another add A global variable is defined in the C file
int a = 2;
//--------------------------------------------------------------------------------
//Variables using other source files in the program need to be declared,
//It's like you need to say hello to others with other people's things
extern int a;

int main()
{
	printf("%d", a);
	return 0;
}

Code 2:

Operation results
Code 1:

Code 2:

And why?

summary
When a global variable is modified by static, the external link attribute of the global variable becomes the internal link attribute.
Therefore, other source files (. cw files) cannot use this global variable.
Code execution process:
Compile link generate executable program

Modifier function

contrast:
Code 1:

//In another add A function is defined in the C file
static int Add(int x, int y)
{
	return x + y;
}
// -----------------------------------------------------
//Declare external functions
extern int Add(int x, int y);

int main()
{
	int a = 2;
	int b = 7;
	int sum = Add(a, b);
	printf("%d", sum);
	return 0;
}

Code 2:

//In another add A function is defined in the C file
static int Add(int x, int y)
{
	return x + y;
}
// -----------------------------------------------------
//Declare external functions
extern int Add(int x, int y);

int main()
{
	int a = 2;
	int b = 7;
	int sum = Add(a, b);
	printf("%d", sum);
	return 0;
}

Operation results:
Code 1:

Code 2:

Summary:
A function originally has an external link attribute, but after being modified by static, the external link attribute becomes an internal link attribute, making other source files (. c files) unusable.

Talk about the remaining keywords slowly.

#define defines constants and macros

#define ADD(x,y) ((x)+(y)) 
int main()
{
	int a = 2;
	int b = 7;
	int sum = ADD(a, b);
	printf("%d\n", sum);
	return 0;
}


The function of the macro is to complete the replacement, replacing the parameter part with the macro body.

(first sight) pointer

Memory

Memory is a very important memory on the computer. The operation of programs in the computer is carried out in memory.

(1) Therefore, in order to use memory effectively, the memory is divided into small memory units, and the size of each memory unit is one byte.
(2) Just like looking for a friend's house, in order to better find his address, the house has a number, which can be found faster and more directly; In order to access each unit of memory more effectively, the memory unit is numbered. These numbers are called the address of the memory unit.

  • The smallest unit of memory in a computer: byte (bit)
  • Bit: 1 bit = 8 byte s
  • Kb: 1 KB = 1024 byte s
  • MB: 1 MB = 1024 KB
  • GB (abbreviated as G): 1 GB = 1024 MB
  • TB (abbreviation T): 1 TB = 1024 GB
  • PB : 1 PB = 1024 TB

Variable: it is created in memory (allocating space in memory), so each memory unit has an address, so the variable also has an address.
The variable address is as follows:

int main()
{
	int num = 7;
	//Take the address of the variable num
	&num;
	//Note: num is an integer with 4 bytes. Each byte has an address. The & address is the address of the first byte (smaller address)
	printf("%p", &num);
	return 0;
}

Operation results:

How to store the address needs to define the pointer variable.
Format for creating pointer variables:
Type name* Variable name = & A variable
Use pointer:
*Variable name found

Definition and use of pointers:

int main()
{
	int num = 2;
	int* pn = &num;
	*pn = 20; //Modify the value of a variable through a pointer
	printf("%d",num);
	return 0;
}

Operation results:

Taking the shaping pointer as an example, it can be extended to other types, such as:

int main()
{
	char ch = 'a';
	char* pch = &ch;
	double dou = 1.2;
	double* pdou = &dou;
	int arr[10] = { 0 };
	int* parr = &arr;
	//Wait
	return 0;
}

Size of pointer variable

The size of the pointer variable depends only on the size of the address

  • On a 32-bit platform, the address is 32 bits (i.e. 4 bytes)
  • On the 64 bit platform, the address is 364 bits (i.e. 8 bytes)

Because the circuit lines of machines with different mechanical bits are different, they can represent different 0 / 1 binary bits. 32-bit machines (32 lines) correspond to 32 bits, which can represent the binary number corresponding to 2 ^ 32 cases, and 64 bit machines are also similar.

int main()
{
	printf("%d\n", sizeof(int*));
	printf("%d\n", sizeof(char*));
	printf("%d\n", sizeof(double*));
	printf("%d\n", sizeof(float*));
	return 0;
}

Running result: (my computer is 32-bit)

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

structural morphology

Structure is particularly important in C language. Structure makes C language have the ability to describe complex types.
For example, describe the student. The student information includes: name, age, gender, student number and so on.
For example:

struct Stu
{
	char name[20];//full name
	int age;//Age
	char sex[7];//Gender
	char id[15];//Student number
};

Initialization of structure:

int main()
{
	//Structure initialization
	//Structure is a special type, so struct Stu is the type name of s1 and s2
	struct Stu s1 = { "Zhang San", 18, "male", "20012725" };
	struct Stu s2 = { "Lily", 19, "woman", "20020147" };
	//. access operators for struct members
	printf("Name:%s Age:%d Sex:%s Id:%s\n", s1.name, s1.age, s1.sex, s1.id);
	printf("Name:%s Age:%d Sex:%s Id:%s\n", s2.name, s2.age, s2.sex, s2.id);
	return 0;
}

Structure is a special type, so struct Stu is the type name of s1 and s2
Operation results:

Write at the end

[I'm a novice learning C language, and a rookie. If there are mistakes or mistakes in writing, please correct and criticize!]
Know the fourth class of C language, record and share what you have learned. Thank you for watching and commenting. Come on!

Tags: C

Posted by Chris16962 on Mon, 18 Apr 2022 04:17:59 +0300