C++:初识

1、变量

语法:数据类型 变量名=变量初始值;
例:int a = 10;(整型)

#include<iostream>
using namespace std;
int main()
{
	//变量创建的语法:数据类型 变量名 = 变量初始值;
    int a=10;
    cout<<"a="<<a<<endl;
	system("pause");
	return 0;
}


2、常量的定义方式:
(1)#define宏常量:#define 常量名 常量值
通常在文件上方定义,表示一个常量
(2)const修饰的变量:const 数据类型 常量名=常量值
通常在变量定义前加关键字const,修饰该变量为常量,不可修改,通常在函数内部

#include<iostream>
using namespace std;
//常量定义的方式
//1、#define 宏常量,通常在函数上方
#define Day 7

int main()
{
    //Day = 14;//错误,Day是常量,一旦修改就会报错
	cout<<"一周总共有:"<<Day<<"天"<<endl;

    //2、const修饰的变量
    const int month=12;
    //month=24;//错误,const修饰的变量也称为常量

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

	system("pause");
	return 0;
}


3、关键字(标识符)

asmdoifreturntypedef
autodoubleinlineshorttypeid

bool

dynamic_cast

int

signed

typename

break

else

long

sizeof

union

case

enum

mutable

static

unsigned

catch

explicit

namespace

static_cast

using

char

export

new

struct

virtual

class

extern

operator

switch

void

const

false

private

template

volatile

const_cast

float

protected

this

wchar_t

continue

for

public

throw

while

default

friend

register

true

delete

goto

reinterpret_cast

try


提示:在定义变量或者常量的时候不能使用关键字
4、标识符命名规则
a、标识符不能是关键字
b、标识符只能由字母、数字、下划线组成
c、第一个字符必须为字母或下划线
d、标识符中字母区分大小写

#include<iostream>
using namespace std;

//标识符命名规则
//a、标识符不能是关键字
//b、标识符只能由字母、数字、下划线组成
//c、第一个字符必须为字母或下划线
//d、标识符中字母区分大小写
int main()
{
    //a、标识符不能是关键字
    //int int = 10;

    //b、标识符只能由字母、数字、下划线组成
	int abc = 10;
    int _abc = 20;
    int _123abc = 30;

    //c、第一个字符必须为字母或下划线
    //int 123abc = 0;

    //d、标识符中字母区分大小写
    int a = 1;
    //cout<<A<<endl;A和a不是同一个名称

   	system("pause");
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mister张!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值