当前位置:网站首页>C语言日记 3 常量
C语言日记 3 常量
2022-08-02 14:03:00 【宇 -Yu】
转义字符:一种特殊的字符常量,用来表示一些不可见字符
有关于常量变量的定义,这里不再赘述。我们只要知道: 不给他
数据有常量和变量组成。
在程序运行过程中,常量的值不能变,变量的值可以变。
\r表示回车:
#include<iostream>
using namespace std;
int main()
{
cout << "zhe\rshi\nyi\ndao\nqian\ndao\nti";
}
#include<iostream>
using namespace std;
int main()
{
cout << "zhe\r";
}
\\表示字符'\',\"表示双引号,\'表示单引号。ASCII字符常量占用1字节。
#include<iostream>
using namespace std;
int main()
{
cout << "zhe\\";
}
符号常量
三种声明形式,例:
1:
#include<iostream>
using namespace std;
int main()
{
const int A=5;
cout << A;
}
2:
#include<iostream>
using namespace std;
int main()
{
int const A = 5;
cout << A;
}
3:
#include<iostream>
using namespace std;
#define A 5
int main()
{
cout << A;
}
or like
#include<iostream>
using namespace std;
#define A 5
int main()
{
int c = 5, Y;
Y = c * A;
cout << Y;
}
在手打该project代码中遇到的问题:
#include<iostream>
using namespace std;
#define A=5 //这里输入=错了,不符合格式
int main()
{
cout << A;
}
书P21例2-2:
#include <iostream>
using namespace std;
void main()
{
int num, total;
const int PRICE = 30; num = 10;
total = num * PRICE;
cout << "total=" << total << endl;
}
#include <iostream>
using namespace std;
#define PRICE 30
void main()
{
int num,total;num=10;
total=num* PRICE;
cout<<"total="<<total<<endl;
}
要注意的是,符号常量与变量不同,它的值在其作用域内不能改变,也不能再被赋值。
使用符号常量的好处是含义清楚,且能做到“一改全改”。
边栏推荐
猜你喜欢
随机推荐
paddle window10环境下使用conda安装
Web Design (Beginners) [easy to understand]
PHP open source customer service system _ online customer service source code PHP
St. Regis Takeaway Notes - Lecture 10 Swagger
Unit 5 Hold Status
[ROS](02)创建&编译ROS软件包Package
drf路由组件Routers
C语言初级—判断一个数是不是素数(函数封装)
【Camera2】由Camera2 特性想到的有关MED(多场景设备互动)的场景Idea
[ROS]ROS常用工具介绍(待续)
Linux:CentOS 7 安装MySQL5.7
STM32 (F407) - stack
C语言sizeof和strlen的区别
C语言字符串——关于指针
第五单元 保持状态
The language of initial
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十三章)
猜数字游戏,猜错10次关机(srand、rand、time)随机数生成三板斧(详细讲解!不懂问我!)
Flask-RESTful请求响应与SQLAlchemy基础
jwt (json web token)