Beginning with C language (3) - keywords, #define, pointers, structures

Continued from the previous article : https://blog.csdn.net/qq_54880517/article/details/121893187

content

common keywords

unsigned keyword

typedef keyword

register keyword -register

static keyword

static modified local variables

static modifies global variables

static decorated function

#define define constants and macros

#define define constants

 #define define macro

pointer

RAM

the address of the variable 

size of pointer variable

 structure

Declaration and initialization of struct types

access to structure members


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

Note: Keywords are preset by the language itself, you cannot create keywords yourself. 

unsigned keyword

The unsigned keyword indicates that the most significant bits of an integer variable represent data bits, not signed bits.

So unsigned int cannot define a negative number 

typedef keyword

Typedef , as the name suggests, is a type definition, which should be understood as type renaming

//将unsigned int 重命名为un_t, 所以un_t也是一个类型名
typedef unsigned int un_t;
int main()
{
   //观察num1和num2,这两个变量的类型是一样的
   unsigned int num1 = 0;
   un_t num2 = 0;
   return 0;
}
 

register keyword -register

If there is a variable to be accessed frequently, it can be recommended to put it in a register

int main()
{
	int a = 10;
	register int b = 5;//建议将5放在寄存器中,放不放取决于编译器
	return 0;
}

static keyword

static modified local variables

The local variable becomes a static local variable. When it enters the scope, it will not be destroyed when it goes out of the scope, and the last value is retained .

Because: static modified local variables are stored in the static area.

The essence is to change the storage location of the variable .

void test()
{
    static int a=1;//a是一个静态的局部变量 
	a++;
	printf("a=%d\n",a);
}
int main()
{
	int i=0;
	while(i<5)
	{
		test();
		i++;
	}
	return 0;
}

void test()
{
    int a=1;//a是一个静态的局部变量 
	a++;
	printf("a=%d\n",a);
int main()
{
	int i=0;
	while(i<5)
	{
		test();	
		i++;
	}
	return 0;
}

static modifies global variables

A global variable originally has an external link attribute, but after being modified by static, the external link attribute becomes an internal link attribute, which can only be used inside the source file where it is located, and cannot be used inside other files.

narrowed the scope

static decorated function

same as modifying global variables

#define define constants and macros

#define define constants

#define MAX 1000
#define STR "hello bit"

int main()
{
	int m = MAX;
	printf("%d\n", m);
	printf("%s\n", STR);
	return 0;
}

 #define define macro

#define MAX(x,y) (x) > (y) ? (x) : (y) 
int main() 
{
	int a = 10;
	int b = 20;
	int m = MAX(a, b);
	printf("较大值:%d\n", m);
	return 0;
}

Just a simple replacement , not the same as a function

#define FUN(x,y) (x)>(y)?(x):(y)
void main()
{
	int i = 10, j = 15,k;
	k = 10 * FUN(i, j);
	printf("%d\n", k);
	k = 10 * 10 > 15 ? 10 : 15;
	printf("%d\n", k);
}

The two spellings are the same. The final result is all 10.

pointer

If you want to understand pointers, you must first understand what memory is and how memory is allocated in the computer.

RAM

All programs in a computer run in memory. Therefore, in order to use the memory efficiently, 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, and these numbers are called the address of the memory unit.

the address of the variable 

We define variables and call functions, all of which are applying for a space in the memory for operation. The address in the memory is the house number of our dormitory. As long as you find the house number, you can be found. Similarly, as long as you find the address of this variable, you can operate on this variable.

How do we find the address of a variable? This has to mention 3 operators & [ ] *

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{
	int num = 2;
	printf("num1=%d\n", num);
	int* pa = &num;
	*pa = 5;
	printf("num2=%d\n", num);
	return 0;
}

The & address here represents the address of the variable; * represents finding the variable according to the address to perform a dereference, and find the content of the variable to operate.

size of pointer variable

The size of the pointer variable depends on the size of the compiler environment. The size of the pointer is four bytes in the 32-bit environment, and the address number is an eight-bit hexadecimal sequence. The size of the 64-bit environment is eight bytes. The number is a hexadecimal sequence of sixteen digits.

 structure

If you encounter a variable with multiple pieces of information, you will use a structure. It can be used to describe a complex object. For example: a person (name + gender + age)

The types of different members of a structure can be the same or different. The difference from an array is that an array is a collection of similar elements, while a structure is a collection of heterogeneous elements.

Declaration and initialization of struct types

struct student
{
	char name[20];//名字
	int age; //年龄
	char sex[5]; //性别
};

just plain struct declaration

int main()
{
	struct student s;
	struct student s1 = { "张三",20,"男" };
	return 0;
}

/*The student here is called the structure name , the struct student is called the type name , the name, age, and sex declared in { } are called structure members, and the following s and s1 are called structure variables . */

Here s and s1 are local variables , and when you want to use global variables, you can also do as follows

struct student
{
	char name[20];//名字
	int age; //年龄
	char sex[5]; //性别
}s;
struct student
{
	char name[20];//名字
	int age; //年龄
	char sex[5]; //性别
}s1,s2,s3;

Because the type name is too long, it can be renamed, and typedef is used .

typedef struct student
{
	char name[20];//名字
	int age; //年龄
	char sex[5]; //性别
}stu;
int main()
{
	stu s2 = { "张三",20,"男" };;
	struct student s3 = { "张三",20,"男" };
	return 0;
}

stu is an alias for struct student

access to structure members

typedef struct student
{
	char name[20];//名字
	int age; //年龄
	char sex[5]; //性别
}stu;
int main()
{
	stu s2;
	struct student s3;
	s2 = { "张三",20,"男" };
	s3 = { "张三",20,"男" };
	return 0;
}

Like the above s2, s3, the method of modifying the member information of the structure is wrong 

Structure member access requires two operators. ->

#include<stdio.h>

typedef struct Student
{
	int age;
	char name[10];
}Stu;

int main()
{
	struct Stu s = { 20,"张三" };
	printf("%d %s\n", s.age, s.name);
	struct Stu* ps = &s;
    ps->age = 22;
    ps->name = "李四";
	printf("%d %s\n", ps->age, ps->name);
    return 0;
}

 . is to operate on structure variables, while -> is to operate on structure pointers

See you next time.

Guess you like

Origin blog.csdn.net/qq_54880517/article/details/121976122