C++ 基础语法

关于程序的注释及变量的意义:

#include<iostream>
using namespace std;

//单行注释

//多行注释

/*main是一个程序的入口
每个程序都必须有这么一个函数
有且仅有一个*/


int main() {
	//变量的定义
	int a = 10;

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

	cout << "hhhhhh" << endl;

	system("pause");

	return 0;
}

关于常量:

#include<iostream>
using namespace std;

//常量的定义方式
//1、#define 宏常量
//2、const修饰的变量(不可修改)

#define day 7

int main() {

	cout << "一周总共有:" << day << "天" << endl;

	const int month = 12;

	cout << "一年总共有:" << month << "月" << endl;

	system("pause");

	return 0;
}

关于数据类型:

#include<iostream>
using namespace std;
#include<string>//用c++风格字符串要包含该头文件

//数据类型存在意义:给变量分配合适的内存空间


int main() {
	//1、整型(short int long longlong)2 4 4 8
	//2、sizeof关键字:可求出数据类型、变量占用内存大小
	//3、实型(浮点型)(float double)4  8
	//4、字符型(char ch = 'a';):字符型变量用于显示单个字符 1
	//5、转义字符 
	//6、字符串型(string)
	//7、布尔类型(bool)1
	//8、数据的输入(cin)
	short num1 = 10;

	cout << "short所占的内存大小:" << sizeof(num1) << endl;

	float f1 = 3.14f;

	cout << "f1=" << f1 << endl;

	double d1 = 4.11;

	cout << "d1=" << d1 << endl;

	float f2 = 3e2;//3*10^2

	cout << "f2=" << f2 << endl;

	char ch = 'a';

	cout << ch <<"   字符型变量所占内存"<<sizeof(char)<<endl;

	cout << (int)ch << endl;  //字符型强制转化为整形

	//换行符\n
	cout << "hello world\n" ;
	//反斜杠
	cout << "//" << endl;
	system("pause");
	//水平制表符\t
	cout << "aaa\thello world" << endl;
	//c风格字符串
	char str[] = "hello world";
	cout << str << endl;
	//c++风格字符串
	string str2 = "hhhhhh";
	cout << str2 << endl;
	//布尔类型
	bool flag = true;
	cout << flag << endl;//true为1 false为0
	//输入数据类型

	int a = 0;
	cout << "请给a赋值" << endl;
	cin >> a;
	cout <<"a="<< a << endl;

	return 0;
}

关于运算符:

#include<iostream>
using namespace std;


int main() {
	//加减乘除
	int a1 = 10;
	int b1 = 3;
	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl;//两个整数相除 结果依然是整数,将小数部分去除

	//两个小数可以相除
	double d1 = 0.5;
	double d2 = 0.2;
	cout << d1 / d2 << endl;//运算结果也可以是小数

	//取模运算(本质就是求余数)
	int a2 = 10;
	int b2 = 3;
	cout << a2 % b2 << endl; //两个小数不能做取模运算

	//递增递减运算符:
	//1、前置递增:先让变量+1,再进行表达式运算
	int a3 = 10;
	int b3 = ++a3 * 10;
	cout << "a3=" << a3 << endl;
	cout <<"b3="<< b3 << endl;

	//2、后置递增:先进行表达运算,再让变量+1
	int a4 = 10;
	int b4 = a4++ * 10;
	cout << "a4=" << a4 << endl;
	cout << "b4=" << b4 << endl;

	//赋值运算符(= += -= *= /= %=)
	int a = 10;
	a %= 2;//a=a%2
	cout << "a=" << a << endl;//0

   //逻辑运算符(!   &&   ||)
   //非!
	int b = 10;
   //在c++中除了0都为真
	cout << !b << endl;
	//&& 与(同真为真,其余为假)
	int c = 0;
	cout <<( b && c) << endl;
	// 或||
	cout << (b || c) << endl;

	system("pause");

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值