LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 119|回复: 0

C++基本的内置类型

[复制链接]
发表于 2024-1-11 17:20:57 | 显示全部楼层 |阅读模式
本帖最后由 一起看海 于 01-11 编辑

C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:

类型               关键字
布尔型           bool
字符型           char
整型              int
浮点型           float
双浮点型       double
无类型           void
宽字符型        wchar_t
一些基本类型可以使用一个或多个类型修饰符进行修饰:

signed
unsigned
short
long

#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;
}
本实例使用了 endl ,这将在每一行后插入一个换行符,<< 运算符用于向屏幕传多个值。我们也使用 sizeof() 函数来获取各种数据类型的大小。

当上面的代码被编译和执行时,它会产生以下的结果,结果会根据所使用的计算机而有所不同:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4typedef 声明
您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:

typedef type newname;
例如,下面的语句会告诉编译器,feet 是 int 的另一个名称:

typedef int feet;
现在,下面的声明是完全合法的,它创建了一个整型变量 distance:

feet distance;


您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表