当前位置:网站首页>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 advanced path - delete duplicates in the sorting array
- Leetcode advanced road - 167 Sum of two numbers II - input ordered array
- Video monitoring system storage control, bandwidth calculation method
- Niuke.com: numbers that appear more than half of the times in the array
- Vertical bar of latex tips absolute value \left\right|
- Redis cache avalanche
- 「运维有小邓」自助帐户解锁工具
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated
- Signal and system review 1
- Software definition boundary (SDP)
猜你喜欢

「运维有小邓」自助帐户解锁工具

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解

Redis缓存雪崩

Interview Essentials - basic knowledge of synchronized underlying principles

分布式服务理论基础

Pytorch deep learning -- neural network convolution layer conv2d

Quick start to VISSIM simulation
随机推荐
牛客网:数组中出现次数超过一半的数字
Software definition boundary (SDP)
Vertical bar of latex tips absolute value \left\right|
获取的网络时间 + 时区(+8)
面试必备——synchronized底层原理的基础知识
「运维有小邓」自助帐户解锁工具
【无标题】破目
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Redis缓存击穿
Leetcode advanced path - the first unique character in a string
蛮力法/邻接表 深度优先 有向带权图 无向带权图
北大青鸟昌平校区:高中学历可以学UI吗?
你的公司会选择开发数据中台吗?
Use DAP link to download the executable file separately to the mm32f5 microcontroller
Heap sorting and hardening heap code for memory
软件测试工程师是做什么的?
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
Brute force method /k integers out of 1~n integers
【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?
Realize OSD reverse color on YUV image according to background color