当前位置:网站首页>C language -- 8 familiar keywords

C language -- 8 familiar keywords

2022-06-10 21:35:00 Try!

1、 keyword

Keywords are C Language provides , You can't create your own keywords ;
Keyword cannot be used as variable name ,eg:int char; This kind of writing is not advisable .

2、 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 unisigned void volatile while The above listed are all common keywords .
(1)auto It's automatic , Every local variable is auto Embellished , Such as : In the structure, there is

{
    
	int a =10;
}

This is actually an automatic creation 、 Automatically destroyed , Is an automatic variable , It is omitted from the front auto, It's actually auto int a =10;
(2)extern: Used to declare external variables
(3)register: Register key

int main()
{
    
	register int num = 100;// Refers to recommendations num Put the value of in the register 
	return 0;
}

In the computer , Data can be stored in “ register ”、“ Cache ”、“ Memory ”、“ Hard disk ”、“ Network disk ” These places .
 Insert picture description here
From bottom to top , The cost is getting higher and higher , The space is getting smaller and smaller , But the speed of reading and writing is getting faster and faster .
take “ A lot of ” perhaps “ frequent ” Data used , Put it in the register , You can improve efficiency .
(4)signed: The signed unsigned: Unsigned
(5)static: Static
In language ,static Is used to modify variables and functions .

  • Modify local variables ---- Static local variables , Changing the life cycle of local variables ( In essence, it changes the storage type of variables )
  • Modify global variable — Static global variables
  • Modify function — Static functions
    1>static Modify local variables
    Statement test function
void test()// Statement test function 
{
    
	int a = 1;
	a++;
	printf("%d",a);
	return 0;
}
int main()
{
    
	int i = 0;
	while (i < 10)
	{
    
		test();
		i++;
	}
	return 0;
}

The running result is :2222222222
 Insert picture description here
If the test Function int a=1; Change it to static int a =1;, Then the running result is 234567891011.
 Insert picture description here
Add :
Memory can be divided into “ The stack area ”( Store local variables )、“ Heap area ”( Dynamic memory allocation ) as well as “ Static zone ”( Store global variables and ststic Decorated static variables ). So in this example ,a From stack area to static area , Its storage type has changed , Further lead to life cycle change . The program doesn't end , Global variables and static Decorated static variables are not destroyed .
2>static Modify global variable
Global variables can be used throughout the project , But be static After decoration, it can not be used in the whole project , If you write in a source file static int g_val=2022;, Write in another source file :
 Insert picture description here
An error will be reported after clicking run , Show unresolved external symbols g_val
static Modifying a global variable will make the global variable only in its own .c In the source file , Other source files cannot be used .
Global variables can be used inside other source files , Because global variables have external link properties , But be static After retouching , It becomes an internal link attribute , Other source files cannot be linked to this static global variable .
3>static Modify function
Write in a source file

int  Add(int x, int y)
{
    
	return x + y;
}

Write in another source file

extern int Add(int x,int y)
int main()
{
    
	int a = 10;
	int b = 20;
	int sum = Add(a,b);
	printf("sum=%d\n",sum);
	return 0;
}

It can be executed in this way , But will int Add(int x, int y) Change it to static int Add(int x, int y) You're going to report a mistake , Show unresolved external symbols .

static Decorate a function so that it can only be used inside its own source file , Cannot be used in other source files , Essentially, static Change the external link property of the function into an internal link property , and static Modify global variables .

(6)struct: Structure keywords
(7)typedef: Type redefinition
The following code is to define an unsigned integer , But it's a little troublesome to write like this , So I used typedef keyword .

int main()
{
    
	unsigned int num = 100;
	return 0;
}

utilize typedef Keyword for type redefinition :

typedef unsigned int u_int;
// It means that unsigned int Rename as u_int, In the following code ,u_int It stands for unsigned integer 
int main()
{
    
	u_int num1 = 100;
	return 0;
}

(8)union: Consortium ( Shared body )
(9)void: nothing , empty
(10)volatile
ask :define And include Is it a keyword ?
answer : They are not keywords , They are preprocessing instructions .

原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101730495407.html