C++ 数据类型

C++ 数据类型

基本的内置类型

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

编号 类型 关键字
布尔型 bool
字符型 char
整型 int
单精度浮点型 float
双精度浮点型 double
无类型 void
宽字符型 wchar_t

类型修饰符

C++ 数据类型修饰符包括signedunsignedlongshort。修饰符 signedunsignedlongshort 可应用于整型, signedunsigned 可应用于字符型, long 可应用于双精度型。修饰符 signedunsigned 也可以作为 longshort 修饰符的前缀。

可以修饰 int   double  char
#include <iostream>
using namespace std;
int main()
{
   short int i;           // 有符号短整数
   short unsigned int j;  // 无符号短整数
   unsigned long int k;
   j = 50000;
   i = j;
   k=j;
   cout << i << " " << j << " " << k;
   return 0;
}

基本数据类型

类型 说明 范围
char 字符类型 1 个字节 -128 到 127 或者 0 到 255
unsigned char 无符号字符 1 个字节 0 到 255
signed char 有符号字符 1 个字节 -128 到 127
int 整型 4 个字节 -2147483648 到 2147483647
unsigned int 无符号整型 4 个字节 0 到 4294967295
signed int 有符号整型 4 个字节 -2147483648 到 2147483647
short int 短整型 2 个字节 -32768 到 32767
unsigned short int 无符号短整型 2 个字节 0 到 65,535
signed short int 有符号短整型 2 个字节 -32768 到 32767
long int 长整型 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int 有符号长整型 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
long long int 长长整形 8个字节
#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 : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4
#include <iostream>
#include <limits>

using namespace std;

int main(){
    cout << "type:char\n";
    cout << "bytes = " << sizeof(char) << "\n\n";

    cout << "type:short\n";
    cout << "bytes = " << sizeof(short) << "\n";
    cout << "smallest value = " << numeric_limits<short>::min() << "\n";
    cout << "largest value = " << numeric_limits<short>::max() << "\n\n";

    cout << "type:int\n";
    cout << "bytes = " << sizeof(int) << "\n";
    cout << "smallest value = " << numeric_limits<int>::min() << "\n";
    cout << "largest value = " << numeric_limits<int>::max() << "\n\n";    

    cout << "type:long";
    cout << "bytes = " << sizeof(long) << "\n";
    cout << "smallest value = " << numeric_limits<long>::min() << "\n";
    cout << "largest value = " << numeric_limits<long>::max() << endl;
    return 0;
}
type:char
bytes = 1

type:short
bytes = 2
smallest value = -32768
largest value = 32767

type:int
bytes = 4
smallest value = -2147483648
largest value = 2147483647

type:longbytes = 8
smallest value = -9223372036854775808
largest value = 9223372036854775807

除了上面的这些基本数据类型,其中整型可以划分为带符号的(signed)和无符号的(unsigned)两种

short、int、long、long long表示有符号的

无符号(unsigned)由于没有负数,所以取值范围和带符号(signed)的也不同:

unsigned short :0~2^16 - 1

unsigned int :0~2^32 - 1

unsigned long :0~2^64 -1

void 类型和指针型(*)

void 类型其实是一种用于语法性的类型,而不是数据类型,

主要用于作为函数的参数或返回值,或者定义 void 指针,表示一种未知类型。无值型字节长度为0, 主要有两个用途:

1)明确地表示一个函数不返回任何值;

2)可以产生一个同一类型指针(可根据需要动态分配给其内存)。

#include <stdio.h>
int main()
{
    void *ptr = NULL; //void 指针
    void *buffer; /*buffer被定义为无值型指针*/
    int *p  = NULL;// 整型指针
    char *cp = NULL;//字符指针
    float *fp = NULL;//浮点指针
    return 0;
}

typedef声明

typedef是用来为复杂的声明定义简单的别名。下面是使用 typedef 定义一个新类型的语法:

本身是一种存储类的关键字,与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中

typedef type newname;

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

typedef int feet;

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

feet distance;
#include <iostream>
#include <string.h>

using namespace std;
typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;
int main( )
{
   Book book;
   strcpy( book.title, "C++");
   strcpy( book.author, "microsoft"); 
   strcpy( book.subject, "computer");
   book.book_id = 1;
   cout << "title : " << book.title << endl;
   cout << "author : " << book.author << endl;
   cout << "subject : " << book.subject << endl;
   cout << "book_id : " << book.book_id << endl;
   return 0;
}

// title : C++
// author : microsoft
// subject : computer
// book_id : 1

定义常量

在 C++ 中,有两种简单的定义常量的方式:

  • 使用 #define 预处理器。
  • 使用 const 关键字。

下面是使用 #define 预处理器定义常量的形式:

#define identifier value

#define预处理器

#include <iostream>
using namespace std;

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE "end"

int main(){

   int area;  
   area = LENGTH * WIDTH;
   cout << area << endl;
   cout << NEWLINE;
   return 0;
   }

typedef和#define区别

typedef定义一种类型的别名,而不是简单的宏替换。

1)typedef 仅限于为类型定义符号名称,#define 不仅可以为类型定义别名,也能为数值定义别名,比如可以定义 1One

2)typedef 是由编译器执行解释的,#define 语句是由预编译器进行处理的。

参考资料

https://www.cjavapy.com/article/1786/

posted @ 2023-10-24 20:27  贝壳里的星海  阅读(29)  评论(0编辑  收藏  举报