当前位置:网站首页>Knowledge of initial C language 2.0
Knowledge of initial C language 2.0
2022-06-25 13:20:00 【LIn_ jt】
Write in the foreword :
This article will briefly introduce the scope and life cycle of variables , Constant , character string , The content such as .
The scope and life cycle of variables :
The scope of a variable refers to the scope of the variable , That is, the code range that limits the availability of this variable . The scope of a local variable is the local range of the variable , Please look at the chart below. :

Here a stay main Function body creation , namely a Can only be used in this brace .

These two pictures are the same reason , Use only within your own limits .
For global variables , Its scope is the whole project , Can be used anywhere , Please look at the chart below. :

You can see , There is no error reported by the program here , Explanatory variable a Can be used anywhere .
Classification of constants
c Language constants are divided into the following categories :
Literal constants
const Modified constant variable
#define Defined identifier constant
Enumeration constants
For literal constants , That's what we often say ,1,2,3,A,B,C Such numbers and letters

const Modified constant variable
about const Decorated variable , We say it has constant properties , But it is still a variable , That is, variables , But not exactly , Why does it have variable properties , Please look at the chart below. :
const Decorated variable a Cannot be modified , When compiling a program, the compiler will also report an error , That is, it has a constant attribute , For variable properties , Please look at the chart below.

We can see , When compiling a program 1 Code can't run past , because vs2019 The syntax of extending arrays is not supported , therefore 1 Code at cannot run through , and 2 The code at can run normally , It also shows that const A modified constant has the properties of a variable .
#define Defined identifier constant
#define The defined identifier constants are usually placed in the header file or at the beginning of the program , The input format is :
namely #define Variable name assignment
And it is also a thorough constant , You can also use... When defining arrays , Please look at the chart below. :
There must be no errors when compiling the program .
Enumeration constants
Enumeration constants need to first create enumeration types , This time you need to use keywords enum, use enum To create enumeration types , for instance , We're going to create gender types , Use the following code to implement
It should be noted that , Here MALE You don't need a semicolon at the end of the statement , It's a comma , At the end of the SECRET There is no semicolon after the statement .
After we create the enumeration type , We can print out the value of the enumeration constant
Please look at the chart below. :

Here MALE Assigned to a variable S, and MALE The value of is actually 0, We can print it out :

If printed FEMALE, And SECRET Value , Then their values are 1,2, You can try .
String and escape characters and comments
“HELLO,WORLD”, For this kind of character enclosed by double quotation marks, we call it string , And the most important thing is , The end flag of the string is ’\0‘, Look at the following procedure :

What do you think arr1 And arr2 It's the same , But are they really the same , Let's look at the results of the program :

2 The number code printed out a garbled code , Why is that , We can open the debug window to see the different .
Find out arr1 Array ratio arr2 There is one more in the array ’\0' character , Oh, yes, we are arr2 Array plus '\0', The printed result is 
Conclusion ,'\0' For string end flag , The end of the string will automatically add ‘\0’ To end the string ,printf And strlen The function reads a string with ‘\0' As an end sign , but ’\0' Will not be read . That is to say strlen(”abc“) The length of the string is 3.
About notes , There are two ways of commenting , about c Language notes , We usually use /**/ To comment , Please look at the chart below.
But there's a drawback to this approach , That is, you cannot nest comments ,

The compiler here reported an error .
The second annotation method is to add two... Before the statement //, This method is fast and practical .

vs You can directly comment all the selected statements .
Escape character :
If we want to print on the screen c:\code\test.c, You can't directly input this string to output , Please look at the chart below.

Here \t Escaped as horizontal tab , Allied , There are also the following escape characters :
\? Use... When writing multiple consecutive question marks , Prevent them from being parsed into three letter words
\' Used to represent character constants '
\“ Double quotation marks used to represent the inside of a string
\\ Used to indicate a backslash , Prevent it from being interpreted as an escape sequence character .
\a Warning characters , Beep
\b Back space
\f Paper in
\n Line break
\r enter
\t Horizontal tabs
\v Vertical tabs
\ddd ddd Express 1~3 Eight octal numbers . Such as : \130
\xdd dd Express 2 Hexadecimal numbers . Such as : \x30 0
among , I would like to focus on \ddd, And \xdd, It has been described above \130, Actually \130 It's octal , The escape character it represents is... Corresponding to its decimal system ASCII The characters of the code table , Please look at the chart below. :


stay strlen When calculating the length of a string , Especially remember ’\ddd' The number in cannot exceed 8, If it exceeds 8, Will calculate... In another way . Here's the picture

The arrow points to \628 in ,\62 Parsed into an escape character , therefore \628 The string length of is calculated as 2.
function :
function , Separate the functions one by one , For example, when we find the maximum of two numbers , You can write functions to implement , When you want to compare in the future, you can call it directly . As shown in the following figure, find two functions with larger values

Array
Array , Is a collection used to store the same elements , First , It introduces how to define
int arr[10] = {1,2,3,4,5,6,7,8,9,10}; This code defines an array , among int It refers to the type of data stored in the array ,[10] Refers to the maximum number of... Stored in the array 10 Elements .
int arr[] = {1,2,3,4,5,6,7,8,9,10}; In this code, you can also [10] Medium 10 Get rid of , At this time, the number of storage elements in the array is the number of initialization .
If the array is not initialized , be arr[] The required number must be placed in the .
It should be noted that , If the array is not fully initialized , Other elements of the array are initialized to... By default 0, Here's the picture

Here, the array initializes only the first element to 0.
Array subscript from 0 Start , That is, if you want to access the elements in the array , Should be accessed by subscript , Here's the picture

The end of this paper .
边栏推荐
- Three lines of code to simply modify the project code of the jar package
- Intercept based on byte length
- 15 basic SEO skills to improve ranking
- Drago Education - typescript learning
- 时间过滤器(el-table)中使用
- 关于猜数字游戏的实现
- 学习编程的起点。
- C# 切换中英文输入法
- KDD 2022 | graphmae: self supervised mask map self encoder
- Solution to Nacos' failure to modify the configuration file mysql8.0
猜你喜欢

初始c语言时的一些知识

Golang keyboard input statement scanln scanf code example

深圳民太安智能二面_秋招第一份offer

初始c语言的知识2.0

Sword finger offer II 029 Sorted circular linked list

关于猜数字游戏的实现

AI assisted paper drawing of PPT drawing

指针,它那些不得不说的题目

Conway's law can not be flexibly applied as an architect?

Explication d'un problème de manuel
随机推荐
It's an artifact to launch a website in a few minutes
德国举行全球粮食安全团结会议
QT mouse tracking
MySQL 学习笔记
MySQL learning notes
AGCO AI frontier promotion (6.25)
三行代码简单修改jar包的项目代码
Summer Ending
买基金在哪里开户安全?还请赐教
OpenStack-----Nova源码分析之创建虚拟机
leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
Germany holds global food security Solidarity Conference
量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
Summary of leetcode linked list problem solving skills
Some knowledge about structure, enumeration and union
About data storage in memory
Component: is to switch between multiple components
. NET in China - What's New in . NET
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
KVM script management - the road to dream