当前位置:网站首页>[C language] keyword
[C language] keyword
2022-06-23 05:07:00 【Ordinary people 1】
author :@ Ordinary person 1
special column :《C Language from 0 To 1》
In a word : In the past , All is prologue
explain : The past is irreparable , The future can change
List of articles
Understand keyword classification
C How many keywords are there in the language ?
In ordinary books , All are 32 individual ( Including this book ), But this is all C90(C89) Standards for . Actually C99 And then I added 5 Key words .
however , Current mainstream compilers , Yes C99 The support is not good , We will default later , Use C90 , The idea that 32 individual
Keywords and their descriptions
auto
Declare automatic variables
short
Declare short integer variables or functions
int
Declare an integer variable or function
long
Declare long integer variables or functions
float
Declare floating-point variables or functions
double
Declare a double precision variable or function
char
Declare character type variables or functions
struct
Declare a structural variable or function
union
Declare shared data type
enum
Declare enumeration type
typedef
Used to alias data types
const
Declare read-only variable
unsigned
Declare an unsigned type variable or function
signed
Declare a symbolic type variable or function
extern
Declare variables are being declared in other files
register
Declare register variables
static
Declare static variables
volatile
Explain that variables can be implicitly changed during program execution
void
Declares that the function returns no value or takes no arguments , Declare no type pointer
if
Conditional statements
else
Conditional statement negates Branch ( And if Continuous use )
switch
For switch statements
case
Switch statement Branch
for
A circular statement
do
The body of a loop statement
while
The loop condition of a loop statement
goto
Jump statements without conditions
continue
End the current cycle , Start next cycle
break
Jump out of current loop
default
In the switch statement “ other ” Branch
sizeof
Calculate the data type length
return
Subroutine return statement ( With parameters , Or without parameters ) The loop condition
Understand variables 、 Definitions and statements
Question 1 : What is a variable
Open up a specific size of space in memory , To hold data
keyword : Memory
Question two : How to define variables ( How to use it? )
int a = 10;
char b = ‘a’;
double c = 3.14;
type Variable name = The default value is
Question 3 : Why define variables ( Why? )
Computers are born to solve the problem of people's lack of computing power . namely , Computers are for calculation .
And calculation , You need data .
And to calculate , Any moment , Not all data should be calculated immediately .
Like : To eat , Not all the meals will be eaten by you immediately . The rice should be eaten mouthful by mouthful , So what you haven't eaten yet , You need to put it on the plate for the time being .
The plates here , Just like variables , Meals are like data in variables .
let me put it another way , Why variables are needed ? Because there is data that needs to be saved temporarily , Waiting for further processing .
that , Why eat with plates ? I'd like to have a bite , Can't you just look in the pot ? Of course. , But it's inefficient .
Because where we eat , And a place to cook , Is the more " far " Of
The essence of variable definition
We now know :
The program runs , It needs to be loaded into memory
Program calculation , You need to use variables
that , Define the nature of variables :
Open up a space in memory , To hold data .( Why must it be memory : Because defining variables , It is also a part of program logic branch , The program has been loaded into memory )
Three keywords
The most generous keyword - auto
Due to introduction auto It's about variables , So we want to start with variables
Classification of variables —— Supplementary content
// local variable : The variables contained in the code block are called local variables . Local variables are temporary . Enter code block , Automatically form local variables , Exit the code block automatically Release .[ Many on the Internet say that the variables in the function are local variables , You can't say wrong , But the statement is inaccurate ]
// Global variables : Variables defined outside all functions , It's called a global variable . Global variables are global .
// Code block : use {} The enclosed area , It's called a code block
#include <stdio.h>
#include <windows.h>
int g_x = 100; // Global variables
int main()
{
int x = 10; // local variable ,main Functions are also functions , There are also code blocks {}
printf("x:%d\n", x);
system("pause");
return 0;
}
Scope of variable —— Supplementary content
// The concept of scope : It refers to the code area of the variable that can be accessed normally
#include <stdio.h>
#include <windows.h>
int main()
{
int x = 10;
if (x == 10)
{
int y = 20;
printf(" Local : x: %d, y: %d\n", x, y);//y Only valid within this code block
}
printf(" Local : x: %d, y: %d\n", x, y); // Report errors ,y Can't be visited
system("pause");
return 0;
}
// local variable : Valid only within this code block // The same as above
// Global variables : During the whole program running , Are effective
#include <stdio.h>
#include <windows.h>
int g_x = 100; // Global variables
void show()
{
printf("show: overall situation : %d\n", g_x); // Can be accessed in any code block
}
int main()
{
show();
printf("main: overall situation : %d\n", g_x); // Can be accessed in any code block , Even modified
system("pause");
return 0;
}
#include <stdio.h>
#include <windows.h>
int g_x = 100; // Global variables
int main()
{
int g_x = 10; // local variable , With the same name as the global
printf("g_x:%d\n", g_x); // The output is local , That is, when part and all have the same name , Preferential local . therefore , This is strongly discouraged
system("pause");
return 0;
}
Life cycle of variable —— Supplementary content
Life cycle concept : It refers to the time range from the definition of the variable to its release , The so-called release , It means that the space once opened up is released
local variable : Enter code block , Form local variables [ Open up space ], Exit code block ," Release " local variable
Global variables : After definition , Throughout the life cycle of the program , This variable is always valid
auto relevant
// How to use : Variables typically defined in code blocks , Local variable , The default is auto Embellished , But generally omit
// By default, all variables are auto Do you ? No , Generally used to modify local variables
// Give me a break : What we'll see later , local variable , Automatic variable , Temporary variable , It's the same thing . We collectively refer to local variables
#include <stdio.h>
#include <windows.h>
int main()
{
for (int i = 0; i < 10; i++)
{
printf("i=%d\n", i);
if(1)
{
auto int j = 0; // Automatic variable
printf("before: j=%d\n", j);
j += 1;
printf("after : j=%d\n", j);
}
}
system("pause");
return 0;
}
The fastest keyword —— register
Actually ,CPU It is mainly the hardware unit responsible for calculation , But for ease of calculation , Generally, the first step is to read the data from memory to CPU Inside , Then you need CPU It has certain temporary data storage capacity . Be careful :CPU It's not the time to calculate , To read specific data to CPU Inside , That's too slow , So modern CPU Inside , Are integrated with a set of hardware called registers , Used to save temporary data .
How to understand ? We can understand through a picture :

distance CPU The closer the storage hardware , The faster the speed. .
The essence of register existence : On the hardware level , Improve the computing efficiency of the computer . Because you don't need to read data from memory
register Modifying variables : As far as possible The modified variable , Put in CPU In the deposit area , So as to achieve the purpose of improving efficiency
The problem is coming. :
So what kind of variables , May adopt register Well ?
- Local ( The overall situation will lead to CPU The register is occupied for a long time )
- Will not be written ( Writing requires writing back to memory , If you need to read the test later ,register What is the meaning of ?)
- High frequency read ( Improving efficiency lies in )
- If you want to use , Please don't use a lot of , Because the number of registers is limited
- Except for the above , One more thing , Namely register Decorated variable , Address not available ( Because it's already in the deposit area , Address is a memory related concept )
#include <stdio.h>
#include <windows.h>
int main()
{
register int a = 0;
printf("&a = %p\n", &a); // Compiler error : error 1 error C2103: On a register variable “&”
// Be careful , Not all compilers report errors here
// Since the students are just beginning to learn , No burden on everyone , We will slowly introduce another compiler later .
system("pause");
return 0;
}
Let's test :

But now we don't need to add the keyword manually , reason : The keyword , Never mind , Because today's compiler , Already very smart , Better code optimization than people . Early compilers needed to specify register, For manual optimization , No need now .
The most unworthy keyword —static
Two conclusions of global variables and functions - Supplementary content
Global variables , Can cross file , Interviewed .
Global function , Can cross file , Interviewed .
Modifying variables
1. Modify global variable , This global variable can only be used in this file .
summary :static Modify global variable , What affects is the concept of scope , Function similar to . The life cycle is constant .
2. Modify local variables
void fun1()
{
int i = 0;
i++;
printf("no static: i=%d\n", i);
}
void fun2()
{
static int i = 0;
i++;
printf("has static: i=%d\n", i);
}
int main()
{
for (int i = 0; i < 10; i++)
{
fun1(); fun2(); }
system("pause");
return 0;
}
// Conclusion :static Modify local variables , The life cycle of the variable becomes the global cycle .( The scope remains unchanged )
// Modify function , This function can only be used within this file .
Conclusion
Ashamed to speak , Today is another rotten day , It was almost twelve o'clock before I knew it , It's really a fast day , Let's finish here first !

边栏推荐
- ICER skills 03design compile
- Less than a year after development, I dared to ask for 20k in the interview, but I didn't even want to give 8K after the interview~
- 轮播图的实现
- Cve-2019-14287 (sudo right raising)
- Actual combat | multiple intranet penetration through Viper
- Abnova blood total nucleic acid purification kit protocol
- Talk about the composite pattern in C #
- 功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
- 【Mac】安全性与隐私中没有任何来源选项
- ICER skill 02makefile script self running VCs simulation
猜你喜欢

With the arrival of intelligent voice era, who is defining AI in the new era?

How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!

超越芯片和AI,硬科技资本为什么越来越“硬核”?

A mvc5+easyui enterprise rapid development framework source code BS framework source code

【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码

Object structure diagram, which can quickly illustrate the internal structure of an object

Talk about the composite pattern in C #

UI自动化定位利器-xpath实战

8年经验之谈:月薪3000到30000,测试工程师的变“行”记

Thesis reading_ Relation extraction_ CASREL
随机推荐
接收传来得文件并下载(简单用法)a标签
Const understanding II
使用teqcplot对teqc 质量分析结果进行可视化展示
Shadertoy basic teaching 02. Drawing smiling faces
Talk about the composite pattern in C #
Go learning record II (window)
MySQL import large files (can be millions or hundreds)
Experience of Tencent cloud installed by Kali
Abnova fluorescent dye 510-m streptavidin scheme
JDBC调用存储过程、MySQL触发器
单行或多行文本溢出,省略号代替
E45: ‘readonly‘ option is set (add ! to override)
Freemodbus parsing 1
ICer技能01正则匹配
Learn to draw Er graph in an article
《微信小程序-基础篇》带你了解小程序的路由系统(二)
"Wechat applet - Basics" takes you to understand the routing system of the applet (2)
Meituan's good article: understand swift, Objective-C and the mixing mechanism from the perspective of precompiling
Three operation directions of integral mall
超越芯片和AI,硬科技资本为什么越来越“硬核”?