学习c++的第一个代码(第一天)

1.hello world 的编写

#include <iostream>

using namespace std;

int main()
{
	//第8行代码的含义就是在屏幕中输出hello world
	cout << "hello world" << endl;		//这里endl是换行的意思

	system("pause");
	return 0;
}

2.变量的定义与打印:

#include <iostream>
using namespace std;

int main()
{

	int a = 10;

	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

3.宏定义和const常量

#include <iostream>
using namespace std;

//常量的定义方式
//1、#define 宏常量
//2、const 修饰的常量

#define Day 7
//Day = 8;	这个宏不能被修改

int main()
{
	cout << "一周总共有" << Day << "天" << endl;
	
	const int month = 12;
	//month = 13;	用const修饰的变量相当于常量不能被修改

	cout << "一年有" << month << "个月" << endl;

	system("pause");
	return 0;
}

4.关键字的注意事项

#include <iostream>
using namespace std;


int main()
{
	int a = 10;
	//int int = 10;	不能用关键字取名;

	system("pause");
	return 0;

}

5.标识符的命名规则(给变量名起名时尽量做到见名知意)
1.标识符不能是关键字
2.标识符是由字母、数字、下划线构成
3.标识符不能是数字开头
4.标识符区分大小写


6.数据类型存在的意义:
给变量分配合理的内存空间避免造成内存浪费

整型:
short:短整型占2字节
int:整形占4字节
long:长整型win下面占4字节Linux下32位占4字节64为占8字节
long long :长长整形占8字节

实型(浮点型):
float:单精度占4字节,7位有效数字
double:双精度8字节,15-16位有效数字

#include <iostream>
using namespace std;


int main()
{
	//默认情况下,输出一个小数会显示6位有效数字

	float a = 3.14f;	//加f的原因是因为避免编译器将它认为是double型的
	double b = 3.14;
	
	//科学计数法
	float c = 3e2;	//e后面是正数代表3*10^2
	float d = 3e-2;	//e的后面是负数代表3*0.1^2

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "float占字节数为" << sizeof(a) << endl;
	cout << "double占字节数为" << sizeof(double) << endl;
	cout << "c = " << c << endl;
	cout << "d = " << d << endl;


	system("pause");
	return 0;
}

7.sizeof关键字(里面可放变量也可以放类型)

#include <iostream>
using namespace std;


int main()
{
	long long a = 20;
	int b = 123;

	cout << "short 占" << sizeof(short) << "字节" << endl;
	cout << "int 占" << sizeof(b) << "字节" << endl;
	cout << "long 占" << sizeof(long) << "字节" << endl;
	cout << "long long 占" << sizeof(a) << "字节" << endl;

 
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值