当前位置:网站首页>4. Const and difine and the problem of initializing arrays with const and define
4. Const and difine and the problem of initializing arrays with const and define
2022-07-28 20:05:00 【A programmer who loves playing badminton】
1.const Constant
const Is a constant variable , Decorated variables cannot be changed .
const int num = 1;
// num = 2;
printf("num=%d", num);If you will num=2 Remove comments from , That is to modify const Value , You're going to report a mistake .
const int num = 1;
num = 2;
printf("num=%d\n", num);
At this time, we associate the definition of array ,int arr[] = ;
We know that variables cannot be placed in brackets when defining an array , but const The defined variable is an unchangeable variable , Can we put const Defined variables ?
The answer is C Not in the file , stay Cpp Medium can .
const int n = 10;
int arr[n] = {1, 2, 3};
for (int i = 0; i <= 9; i++)
{
printf("%d", arr[i]);
}
return 0;This code is written in C In file

Written in Cpp In file

Why does it appear in this problem ?
First, we need to understand what constants are , What is a read-only variable ?
- Constant It must be read-only , For example, digital 6, character string “abc” etc. , It must be read-only , because There is no place in the program to store its value , Of course, we can't modify it .
- A read-only variable It is in Open a place in memory to store its value , Only this value is limited by the compiler Not allowed to be modified .
- C Language Subscripts must be constants when defining arrays , Read only variables are not allowed .
therefore
stay C In language ,const It is a modifier used to limit that a variable is not allowed to be changed , Read only variable , Because it occupies storage space , So the compiler doesn't know the value at run time , So I don't know how big the array should be defined .
stay C++ in , const The modified can be regarded as a constant at compile time
For basic data types : The compiler will put it in the symbol table , Without allocating storage space .
about ADT(Abstract Data Type Abstract data types )/ UDT( User defined type ) Of const Objects need to allocate storage space ( Big object ).
And in some cases, it is also necessary to allocate storage space , For example, the mandatory declaration is extern Or get the address of the Symbolic Constant .
So how to C As in the C++ Then you can define the array ? Let's see define
2.define
define Used to define identifier constants , It's also called hongdefination , The program will use define Defined content Replace
for example define N 100, Will be 100 To replace all N, In other words , differ const Constant ,define In fact, it is essentially the replacement of text content .
So in C You can use define To initialize an array
#define MAX 10
int main(int argc, char const *argv[])
{
int arr[MAX] = {0};
printf("%d", MAX);
return 0;
}It won't make a mistake .
Sum up , Let's see const and define The difference between
1.define Is a macro definition , The program will use define Replace the defined content . So when the program is running , There's no use in the constant table define Defined constant , The system does not allocate memory for it . and const Defined constant , When the program is running , Exists in the constant table , And The system allocates memory for it .
2.define Defined constant , The preprocessing is just a direct replacement , Therefore, data type verification cannot be performed at compile time .
and const Defined constant , stay Strict type checking at compile time , You can avoid mistakes .
tips:
We are choosing define still const When initializing the array , If it says C Language , Then use define, And in the c++ It is more recommended to use const, because define In the pretreatment stage Lack of type detection mechanism , Therefore, the type cannot be specified correctly , It will lead to some mistakes . and define Preprocessing macros are global . So in C++ So much emphasis on namespaces 、 In the language of such things , The best thing for the overall situation is to have as few things as possible .
边栏推荐
- Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
- [network] cross area network communication learning classification and calculation of IPv4 address
- adb remount of the / superblock failed: Permission denied
- 冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
- Integration and implementation of login click graphic verification code in personal blog system
- English Translation Spanish - batch English Translation Spanish tools free of charge
- ssm中项目异常处理
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- [C language] guessing numbers game [function]
- Basic concept and essence of Architecture
猜你喜欢

毕马威中国:证券基金经营机构信息技术审计项目发现洞察

数字滤波器设计——Matlab

Intermediate soft test (system integration project management engineer) high frequency test site

你知道雨的类型有几种?
![[C language] Hanoi Tower problem [recursion]](/img/d8/ff66928c2bc2ad906e38a360a8cf94.png)
[C language] Hanoi Tower problem [recursion]

Idea properties file display \u solution of not displaying Chinese

时间转日期的sql语句应该怎么写?

Codeignier framework implements restful API interface programming

数字图像理论知识(一)(个人浅析)

克服“看牙恐惧”,我们用技术改变行业
随机推荐
Cell review: single cell methods in human microbiome research
[in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
[experience] some suggestions and experience on repairing electronic equipment
党员故事|李青艾用漫画带动农民增收致富
MySQL8 Status Variables: Internal Temporary Tables and Files
C+ + core programming
CodeIgnier框架实现restful API接口编程
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
克服“看牙恐惧”,我们用技术改变行业
基于C语言的信息管理系统和小游戏
Why is there no log output in the telnet login interface?
Implementation of strcat in C language
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Advanced notes (Part 2)
NetCoreAPI操作Excel表格
leetcode day5 删除重复的电子邮箱
Redis笔记
Servlet学习笔记
[C language] scanf format input and modifier summary
Andorid system layout, values, drawable adaptation