提示:文章# C++学习
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
- 一、C++标识符
- 二、C++注释
- 三、C++数据类型
- 总结
一、C++标识符
C++ 标识符是用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。
注:即不以数字开头以及不与关键字相同。
下表列出了 C++ 中的保留字。这些保留字不能作为常量名、变量名或其他标识符名称。
| asm | else | new | this |
|---|---|---|---|
| auto | enum | operator | throw |
| bool | explicit | private | true |
| break | export | protected | try |
| case | extern | public | typedef |
| catch | false | register | typeid |
| char | float | reinterpret_cast | typename |
| class | for | return | union |
| const | friend | short | unsigned |
| const_cast | goto | signed | using |
| continue | if | sizeof | virtual |
| default | inline | static | void |
| delete | int | static_cast | volatile |
| do | long | struct | wchar_t |
| double | mutable | switch | while |
| dynamic_cast | namespace | template |
同时:C/C++可以使用带有 $ 的标识符:
#include "stdio.h"
int main(){
int $8=0x99; //它甚至可以这样
printf("%dn", $8);
return 0;
}
二、C++注释
- 在代码前加"//",这种注释仅能注释此符号后内容,不跨行。
- 将代码包括""这两个符号之间,可以跨行,但不能嵌套使用。
- 此外,#if … #endif 虽属于条件编译,却也可以作为注释,如:
#if 0 code #endif三、C++数据类型
C++中七种基本的数据类型有bool ,int ,char ,float ,double , void , wchar_t 。
其实 wchar_t 是这样来的:typedef short int wchar_t; 所以wchar_t的申请空间和short int 一样大的
一些基本类型可以使用一个或多个类型修饰符进行修饰:
- signed … 有符号
- unsigned … 无符号
- short …短
- long…长
变量的大小会根据编译器和所使用的电脑而有所不同。
下面实例会输出您电脑上各种数据类型的大小。
#include#include using namespace std; int main() { cout << "type: tt" << "************size**************"<< endl; cout << "bool: tt" << "所占字节数:" << sizeof(bool); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "char: tt" << "所占字节数:" << sizeof(char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "signed char: t" << "所占字节数:" << sizeof(signed char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "short: tt" << "所占字节数:" << sizeof(short); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "int: tt" << "所占字节数:" << sizeof(int); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long: tt" << "所占字节数:" << sizeof(long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "double: t" << "所占字节数:" << sizeof(double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long double: t" << "所占字节数:" << sizeof(long double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "float: tt" << "所占字节数:" << sizeof(float); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "size_t: t" << "所占字节数:" << sizeof(size_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "string: t" << "所占字节数:" << sizeof(string) << endl; // << "t最大值:" << (numeric_limits ::max)() << "t最小值:" << (numeric_limits ::min)() << endl; cout << "type: tt" << "************size**************"<< endl; return 0; }
typedef int bao:为int类型定义了一个新名字为bao,实际上他们是同一种类型。
如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型:
enum 枚举名{
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数]
} 枚举变量;
例如,下面的代码定义了一个颜色枚举,变量 c 的类型为 color。最后,c 被赋值为 “blue”。
enum color { red, green, blue } c;
c = blue;
因为没有对red,green,blue这几个整型元素初始化,故默认red = 0,后面依次加 1 。
总结感谢菜鸟教程!



