当前位置:网站首页>C语言的常数知识讲解
C语言的常数知识讲解
2022-07-26 22:49:00 【光光光仔】
常量
C语言中包括常量和变量,今天讲解一下C语言中的常量。
常量分为以下几种:
(1)字面常量:通常直接写出来的不变的量(可以是整数,浮点数,字符,字符串)
#include<stdio.h>
int main()
{
123456; //字面常量
5.21; //字面常量
‘a’; //字面常量
"abcd"; //字面常量
return 0;
}(2)const修饰的常变量(顾名思义具有变量的特性但仍然是常量)
#include<stdio.h>
int main()
{
const int a = 5;
a = 6;//这里a的值不会发生改变,a为变量但是在const的修饰下,a的值确定了,无法发生改变,为常量。
int m[a] = {0,1,2,3};//a这里具备变量的性质,这是在部分编译器中错误的表达定义数组
}(3)#define定义的标识符常量(宏定义,在主函数之前定义)
#include<stdio.h>
#define PI 3.14
#define MAX 10
int main()
{
int a = 0;
a = PI*PI; //PI的值可以直接用
int arr[MAX] = {0};//MAX相当于常量10
printf("%d",a);
printf("%d",arr[1]);
return 0;
}(4)枚举常量(有固定的格式,一般生活常见的性别,三原色等可以列举出来的,可以用枚举)
enum SEX //性别有男女之分 可以数的清
{
nan, //枚举常量的组合 这里nan 默认为0,第一个(nv也可以在第一个)
nv //默认为1
};
int main()
{
printf("%d\n", nan); //输出为0
printf("%d\n", nv); //输出为1
return 0;
}以上就是C语言的常量知识的解释了。
求三连,加关注噢!
边栏推荐
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
- OSPF协议知识汇总
- HandsomeForum学习论坛
- Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
- 广域网技术实验
- 第五讲—按键控制LED
- ensp中的简单静态路由
- CF 1333C Eugene and an array
- Golang bufio Reader 源码详解
猜你喜欢

7.13 Weilai approved the written examination in advance

Brief introduction of VLAN principle and specific experimental configuration

Test and open basic daily question brushing (continuous updating...)

机械硬盘选购指南——从选购经历谈起

ESP8266Wi-Fi数据通讯

HCIA Basics (1)

MySQL课程1.简单命令行--简单记录 欢迎补充纠错

NB-IOT联网通信

ESP8266Wi-Fi接入云平台

(title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets
随机推荐
C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句
6.29 Zhong'an Summer Internship
Codeforces Round #810 (Div. 2), problem: (B) Party
OGeek Meetup第一期,携手CubeFS火热来袭
C语言——字符和字符串、算术运算符、类型转换
全网显示 IP 归属地,是怎么实现的?
6.28 flush written test
RS-485 bus communication application
Test and open basic daily question brushing (continuous updating...)
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
动态路由ofps协议配置
JS -- first understand the naming rules and data types of JS and variables
Influence of pre frequency division value and automatic reload value on interrupt frequency
Golang — 解析 yaml 文件
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
微信小程序:用户微信登录流程(附:流程图+源码)
NAT (network address translation protocol)
First acquaintance with C language (1)
7.13 Weilai approved the written examination in advance
预分频值和自动重装值对中断频率的影响