1.基本语法
1.1. first program
#include <iostream>
using namespace std; //命名空间
int main(){
cout << "Hello World"; // 输出 Hello World return 0;
}
$ g++ hello.cpp
$ ./a.out
Hello World
2. 数据类型
布尔型 bool
字符型 char
整型 int
浮点型 float
双浮点型 double
无类型 void
宽字符型 wchar_t
2.1. 数据类型大小
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
/*
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
*/
2.2. 枚举类型
枚举类型声明一个可选的类型名称和一组标识符,用来作为该类型的值。
其带有零个或多个标识符可以被用来作为该类型的值。
每个枚举数是一个枚举类型的常数。
创建枚举,需要使用关键字 enum。枚举类型的一般形式为:
enum enum-name { list of names } var-list;
enum color { red, green, blue } c;
c = blue;
enum color { red, green=5, blue };
/*默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。
*可以给名称赋予一个特殊的值,只需要添加一个初始值即可。例如,在下面的枚举中,green 的值为 5
*在这里,blue 的值为 6,因为默认情况下,每个名称都会比它前面一个名称大 1。
*/