C++ 整型变量

变量名

  • 只能用字母、数字、下划线
  • 第一个字符不能是数字
  • 区分大小写
  • 不能使用关键字

整型

  • short 16位
  • int 32位
  • long 32位
  • long long 64位

limits.cpp

#include <iostream>
#include <climits> // 定义了符号常量来表示类型的限制
using namespace std;
int main()
{
     int n_int = INT_MAX; // 将n_int初始化为int能够存储的最大值
     short n_short = SHRT_MAX; // #define SHRT_MAX 32767
     long n_long = LONG_MAX;
     long long n_llong = LLONG_MAX;

     // 使用sizeof计算变量类型的大小
     cout << "int is " << sizeof(int) << " bytes" << endl;
     cout << "short is " << sizeof(short) << " bytes" << endl;
     cout << "long is " << sizeof(long) << " bytes" << endl;
     cout << "long long is " << sizeof(long long) << " bytes" << endl;

     // 表示整型变量能表示最大值
     cout << "Maximum values:" << endl;
     cout << "int: " << n_int << endl;
     cout << "short: " << n_short << endl;
     cout << "long: " << n_long << endl;
     cout << "long long: " << n_llong << endl;

     // 表示整型变量能表示最小值
     cout << "Minimum int value = " << INT_MIN << endl;
     cout << "Bits per byte = " << CHAR_BIT << endl; // char的位数
     return 0;
}

输出

 int is 4 bytes
 short is 2 bytes
 long is 4 bytes
 long long is 8 bytes

 Maximum values:
 int: 2147483647
 short: 32767
 long: 2147483647
 long long: 9223372036854775807

 Minimum int value = -2147483648
 Bits per byte = 8
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光开朗男孩

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值