当前位置:网站首页>[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 !

边栏推荐
- Meituan's good article: understand swift, Objective-C and the mixing mechanism from the perspective of precompiling
- Pads and flash symbols in cadence
- The solution to prompt "this dictionary creation could be rewritten as a dictionary literal" when updating the dictionary key value in pychart
- Using editor How to handle MD uploading pictures?
- 传统意义上的互联网式的平台或将不复存在,一个融合的产业特质和互联网特质的全新产业
- ICer技能02makefile脚本自跑vcs仿真
- [graph theory] - bipartite graph
- JSP入门级笔记
- rtklib2.4.3 b34 单点定位的一个bug
- QT elidedtext only works for Chinese conformity, but not for English
猜你喜欢

② Cocoapods principle and podspec file uploading operation

Abnova actn4 purified rabbit polyclonal antibody instructions

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

Talk about the composite pattern in C #

ICER skill 02makefile script self running VCs simulation

Dsp7 environment

Brief ideas and simple cases of JVM tuning - why do you need JVM tuning?

工作5-8年的外包测试员,平均年薪有多少?

美团好文:从预编译的角度理解Swift与Objective-C及混编机制

精密星历介绍与使用
随机推荐
Course design C for freshmen -- clothing management system
GNSS速度解算的三种方法
Li Kou today's question 513 Find the value in the lower left corner of the tree
UI自动化定位利器-xpath实战
Dsp7 environment
微信小程序:未来老婆查询生成器
MySQL存储过程
Left and right values
Pads and flash symbols in cadence
Const understanding II
数据科学家是不是特有前途的职业?
Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection
MVVM has become history, and Google has fully turned to MVI
Three operation directions of integral mall
Chrome debugging tips
怎样利用数据讲一个精彩故事?
MySQL import large files (can be millions or hundreds)
DO280OpenShift命令及故障排查--常见故障排除和章节实验
Reinstallation of cadence16.3, failure and success
What are the main aspects of visual improvement brought by introducing AI into ISP Technology