当前位置:网站首页>C language -- 4 first-time constant
C language -- 4 first-time constant
2022-06-10 21:29:00 【Try!】
1、 Classification of constants
C Constants in languages are divided into the following categories :
- Literal constants , Such as :10,3.14,“abc”,'a’ etc.
- const Constant decorated
- #define Defined identifier constant
- Enumeration constants
2、const Constant decorated
#include <stdio.h>
int main()
{
int num = 10;// Variable is num
num = 20;
printf("%d\n",num);
return 0;
}
The running result is :20. On this basis will int num = 10; Change it to const int num = 10;, Then run time num=20 There will be an error :
Because you add const The value of the variable is given dead , This value cannot be modified , At this time num It's called “ Constant variable ” instead of “ Variable ” 了 , Has constant attribute ( Properties that cannot be changed ), But still pay attention to , It is also different from constants , Let's verify that it is not the same as a constant .
stay int n = 10; prefix const I still report mistakes , Show “ The expression must contain a constant value ”. So const The modified variable is a constant variable .
3、#define Defined identifier constant
#include <stdio.h>
#define MAX 1000;
int main()
{
int n = MAX;
printf("%d\n",n);
return 0;
}
The running result is 1000.
4、 Enumeration constants
Enumerated constants refer to constants that can be enumerated one by one , Such as : When you log in to some software and fill in some information , You can choose male sex 、 Woman , You can also choose “ Private ”.
Want to use enum Keyword to define gender type .
#include <stdio.h>
enum sex
{
male,
female,
secret
};// This contains all possible values of the enumeration type
int main()
{
enum sex s = male;
printf("%d\n", male);//male It refers to any possible value
printf("%d\n", female);
printf("%d\n", secret);
return 0;
}
The running result is :
0
1
2
The reason for this running result is : Enumeration constants are constants , Its value starts from zero , The default is 0 At the beginning .
边栏推荐
- 堆排序与加强堆代码 用于记忆
- 初中毕业生,选择中职学校也可以升入高等学府
- LeetCode 进阶之路 - 删除排序数组中的重复项
- MySQL service startup failed
- H.264中NALU、RBSP、SODB的关系
- Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
- [generation confrontation network learning part I] classic Gan and its existing problems and related improvements
- ROS virtual time
- Calculus review 1
- Heap sorting and hardening heap code for memory
猜你喜欢

A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb

Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test

Quick start to VISSIM simulation

Fast Planner - detailed explanation of kinetic astar

Can you still have a wonderful life if you are laid off at the age of 35?

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)

初中毕业生,选择中职学校也可以升入高等学府

学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?

你的公司会选择开发数据中台吗?

Asynchronous, thread pool (completablefuture)
随机推荐
Asynchronous, thread pool (completablefuture)
Read the source code of micropyton - add the C extension class module (1)
Redis cache penetration
H265 Nalu type judgment and SPS data analysis
Fast Planner - detailed explanation of kinetic astar
学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?
面试必备——synchronized底层原理的基础知识
「运维有小邓」自助帐户解锁工具
Redis cache avalanche
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
Identity and access management (IAM)
Nanny tutorial: how to become a contributor to Apache linkis documents
【无标题】破目
H265 Nalu类型判断及 sps 数据解析
Redis缓存雪崩
[untitled] broken item
Construction of RT thread smart win10 64 bit compilation environment
"O & M youxiaodeng" self service account unlocking tool
Leetcode advanced path - the first unique character in a string
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"