Getting to know C language for the first time —— 4

Article directory

  • common keywords
  • 1. The keyword typedef

  • 2. The keyword static

  • define defines constants and macros
  • pointer
  • structure


foreword

This is the last article in the blogger's first introduction to C language series. After that, the blogger will update more detailed knowledge about C language learning. I hope you guys will support me a lot.

1. Common keywords

1. The keyword typedef

typedef, as the name suggests, is a type definition, and here it should be understood as a type renaming.

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

Simply put, it is to rename a complex type for ease of use.

2. The keyword static

In C language: static is used to modify variables and functions:

1>. Modified local variables - called static local variables

2>. Modified global variables - called static global variables

3>. Modified function - called static function

partial 

When static modifies local variables
Originally a local variable is stored in the stack area, if it is modified by static, it will be stored in the static area
Static modification of local variables changes the storage type (location) of the variable, making the life cycle of this static variable longer until the end of the program
but the scope remains the same

Compare the effect of code 1 and code 2 to understand the significance of static modification of local variables.

We can draw such a conclusion: Static modification of local variables changes the life cycle of variables, so that static local variables still exist outside the scope, and the life cycle ends when the program ends. 

 global

static modifies global variables
Global variables have external link attributes, so they can still be used inside other source files (the method must be correct)
Static modifies the global variable, changing the link attribute of this global variable, from the external link attribute to the internal link attribute
This static variable can only be used inside the source file where it is located, and cannot be used inside other source files
It feels like the scope has become smaller

function 

static modifier function
Static modified functions are the same as static modified global variables
Functions have external link attributes, but if they are modified by static, they become internal link attributes
So that this function can only be used inside the source file where it is located, and cannot be used inside other files

2. define defines constants and macros

define defines a constant

define define macro 

  

3. Pointer

To understand pointers, let's first understand memory:

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

Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses. Take out the variable address as follows:

#include <stdio.h>
int main()
{
 int num = 10;
 &num;//取出num的地址
    //注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
 printf("%p\n", &num);//打印地址,%p是以地址的形式打印
 return 0;
}

 How to store the address requires defining a pointer variable.

int num = 10;
int *p;//p为一个整形指针变量
p = &num;

&num - pointer

The pointers we speak in spoken language are basically pointer variables.

A pointer is actually an address.

#include
//%p - 专门用来打印地址的,是16进制的形式表示地址的
int main()
{
	int a = 15;//虽然a占有4个字节,但是当我们&a的时候,拿到的是4个字节中第一个字节的地址
	//printf("%p\n", &a);

	int * pa = &a;//pa存放a的地址的,是一个变量,叫:指针变量,意思是存放指针的变量
	*pa = 30;//解引用操作符,*pa 就是a
	
	printf("%d\n", a);
	return 0;
}

Let's test the size of the pointer variable: 

The blogger uses a 32-bit platform, so 4 is printed out. 

4. Structure

The structure is a particularly important knowledge point in the C language. The structure makes the C language capable of describing complex types.

For example, to describe a student, the student includes: name + age + gender + student number.

Here we can only use structure to describe.

For example:

struct Stu//结构体类型
{
    char name[20];//名字
    int age;      //年龄
    char sex[5];  //性别
    char id[15]; //学号
};

 Here is the second printing method:

struct S
{
	char name[20];
	int age;
	float score;
};

void print1(struct S t)
{
	printf("%s %d %f\n", t.name, t.age, t.score);
}

void print2(struct S* ps)
{
	printf("%s %d %f\n", (*ps).name, (*ps).age, (*ps).score);
	printf("%s %d %f\n", ps->name, ps->age, ps->score);
}

//结构体变量.成员
//结构指针->成员

int main()
{
	struct S s = { "zhangsan", 20, 85.5f };
	print1(s);
	print2(&s);

	return 0;
}

Dear veterans, this is the end of the blogger's first acquaintance with the C series. Follow-up bloggers will update more detailed C language learning knowledge. If the blogger's article is helpful to you, please pay attention to it, like it, and support the blogger. Thank you for your attention and likes.

Guess you like

Origin blog.csdn.net/LXW0403/article/details/130160374