C的关键字说明和使用
C++的32个关键字
1.auto
最开始的auto基本是没有什么用的,C++11中的auto是非常的好用的,自动类型转化。
可以查看我的另外的博客,这里就不去细说。
2.char
定义一个字符型变量。字节为1,8个比特位。表示的是数是255。
3.short
定义一个整形型变量。字节为2,16位。
4.int
定义一个整形型变量。字节为4,32位。
5.long
定义一个浮点型变量。字节为8,64位。
6.float
定义一个浮点型变量。字节为4,32位。
7.double
定义一个双精度浮点型变量。字节为8,64位。
8.signed
是有符号的意思,简单理解就是,写上这个符号就可以表示符号,可以不要去记住。因为默认是signed的。
8.unsigned
如果要表示非负数的时候就要增加这个符号,增加这个符号之后,就没有了负数的部分,所以表示的最大数是有符号数的两倍。
9.enum
枚举型变量,变量的默认初始值是0,是+1递增的。
10.union
联合体,在编程的时候是很少用到的,联合体中的字节数是最大的哪一个。在使用的是时候,用来当一个指针的联合体,因为指针的字节数都是一样的
11.struct
结构体,就是自己自定义一个数据的集合。
struct student
{
int age;
char name[16];
};
这个就是学生体的集合,包含着一些有关的信息。
12.循环的while,do
do {} while();
先执行一次在做while里面的条件的循环,正确走{}的内容,错误就跳出。
do
{
}while()
while(){}
如果满足()里面的条件,就会一直走{},走一次判断一次()的条件。
while()
13.循环的for
是通过次数去控制的循环
for(int i=0;i<10;i++)
{
只能是整数
}
循环可以迭代的对象
std::vector<int>ve_data;
for(auto elem:ve_data)
{
//循环遍历了容器中的对象
}
14分支判断if else
怎么理解分支判断呢?简单点讲,就是满足那个条件就走哪一个{ }的处理,如果那个都不能满足,那么那个都不走。
if()
{
//满足()中内容就进入,不满足继续后面的代码
}
if(a>5)
{
//满足()的条件进入,不满足就进入else中
}
else
{
}
//多个判断,满足那个就走那个
if(){}
else if(){}
else if((){}
15分支判断switch case default break
switch case只能用来判断int类型的数据和char类型的是否满足条件
int n;
switch(n)
{
case 1:break;
case 2:break;
case 3;braak;
default:
}
16循环控制continue break
循环进行的时候,无非就是结束循环和跳过这次的循环。
continue就是用来跳个当前的循环。
break是用来结束当前的循环的。
17函数的返回值 return
函数的返回值用return返回,返回对应的数据类型就行。
void test()
{
int a=100;
return ;
}
int test1()
{
int a=100;
return a;
}
18计算字节数sizeof()
使用的时候类型函数,但是是关键字。
sizeof(int);
自己定义类型typedef
typdedef int uint;//就是起别名的意思,uint就是int
uint a=100;//等价于int a=100;
测试
int main()
{
//1.auto
auto a = 10;
auto b = 1.0;
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
cout << "---------------------" << endl;
//2.char:只能接收一个字符,如果字符超过了,只会保留最后一个字符
char cha = '1';
char chb = '12';
char chc = 'abc';
cout << "sizeof(char):" << sizeof(char) << endl;
cout << cha << endl;
cout << chb << endl;
cout << chc << endl;
cout << "---------------------" << endl;
//3.short:如果超过了最大值,数据就不正确了
cout << "sizeof(short):" << sizeof(short) << endl;
short sha = 65534;
short shb = 65535;
unsigned short shc = 65538;
cout << sha << endl;
cout << shb << endl;
cout << shc << endl;
cout << "---------------------" << endl;
//4.int,long类似
cout << "sizeof(int):" << sizeof(int) << endl;
cout << "sizeof(long):" << sizeof(long) << endl;
cout << "sizeof(long long):" << sizeof(long long) << endl;
cout << "---------------------" << endl;
//5.float double
float fla = 1;
double doa = 10.0;
printf("%f\n", fla);
printf("%lf\n", doa);
std::cout << "---------------------" << endl;
//enum 枚举类型中数据只能是枚举类型或者int类型
enum e {i,o,p};
enum num
{
one, tow,e,five=5,six
};
std::cout << one << endl;
std::cout << e << endl;
cout << six << endl;
std::cout << "---------------------" << endl;
//struct
struct student
{
int age;
char name[16];
};
student stu;
stu.age = 18;
strcpy_s(stu.name, "zhangsan");
//strncpy(stu.name, "zhangsan",sizeof("zhangsan"));
cout << stu.age << endl;
cout << stu.name << endl;
std::cout << "---------------------" << endl;
//union
union TypeUnion
{
char uchar;
short ushort;
int uint;
long ulong;
long long ulonglong;
};
std::cout << sizeof(union TypeUnion) << endl;//是8,是longlong的
int aa = 0x11223344;
TypeUnion type;
type.uint= aa;
printf("%2x\n", type.uchar);
cout << hex << type.ushort << endl;
cout << type.uint << endl;
union typePoint
{
char* uchar;
short* ushort;
int* uint;
long* ulong;
long long* ulonglong;
};
std::cout << sizeof(union typePoint) << endl;//是8,是longlong的
typePoint point;
point.uint = &aa;
cout << hex << &aa << endl;
cout << hex << (void*)point.uchar << endl;
cout << point.ushort << endl;
cout << point.uint << endl;
std::cout << "---------------------" << endl;
//cout输出字符串的地址时候需要做类型的强制转化,因为cout实现打印字符串的时候,
//给的是字符串的首字符的地址,如果直接给char*的地址的话相当于打印字符串了。
//如:
char chp[10] = "1234567";
cout << chp << endl;//a就是字符数组的首地址
cout << &chp << endl;
cout << &chp[0] << endl;
return 1;
}