当前位置:网站首页>C语言日记 4 变量
C语言日记 4 变量
2022-08-02 14:03:00 【宇 -Yu】
赋值:
例:
#include<iostream>
using namespace std;
int main()
{
int a,b,c = 5;
}
即a,b没有赋初值,c赋初值为5。
在给同一类型不同变量赋值时,注意中间必须用逗号而不要用分号。例:
#include <iostream>
using namespace std;
void main()
{
int x = 5; r = 8; a= 9; b = 10;
cout << (x + r) * 8 - (a + b) / 7;
}

正确方式:
#include <iostream>
using namespace std;
void main()
{
int x = 5, r = 8, a= 9, b = 10;
cout << (x + r) * 8 - (a + b) / 7;
}
在定义中不允许连续赋值,例:(不在定义中连续赋值就不知道是什么情况了,反正我们现在也不知道什么时候不在定义中赋值)
#include<iostream>
using namespace std;
int main()
{
int a=b=c=5;
}
结果:

书本P20例2-1:
#include<iostream>
using namespace std;
int main()
{
int a = 3, b, c = 5;
b = a + c;
cout << "a=" << a << " , b = " << b << " , c = " << c << endl;
}
结果:
a=3 , b = 8 , c = 5
赋值的另一种形式,例:
#include<iostream>
using namespace std;
int main()
{
int a(5);
cout << a;
}
等价于int a=5;
本例中遇到过的问题:
#include<iostream>
using namespace std;
int main()
{
int a(5);
cout << a;
}

错误原因: int a(5):采用中文输入导致运行失败
边栏推荐
- Unit 11 Serializers
- chapter7
- yolov5,yolov4,yolov3乱七八糟的
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十四章)
- [ROS] (05) ROS Communication - Node, Nodes & Master
- [ROS] Introduction to common tools in ROS (to be continued)
- 云GPU(恒源云)训练的具体操作流程
- Creating seven NiuYun Flask project complete and let cloud
- 第十单元 前后连调
- Linux: CentOS 7 install MySQL5.7
猜你喜欢
随机推荐
瑞吉外卖笔记——第05讲Redis入门
redis延时队列
drf路由组件Routers
Building and getting started with the Flask framework
ftp常用命令详解_iftop命令详解
Go语言初始
【Camera2】由Camera2 特性想到的有关MED(多场景设备互动)的场景Idea
瑞吉外卖笔记——第08讲读写分离
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
第十五单元 分页、过滤
Unit 8 Middleware
第十单元 前后连调
STM32(F407)—— 堆栈
关于密码加密的一点思路
鼠标右键菜单栏太长如何减少
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十一章)
第七单元 ORM表关系及操作
paddleocr window10初体验
C语言字符串——关于指针
Unit 12 associated serialization








