(limits.h) 在 C/C++ 中


头文件“limits.h”存在于 C 语言中,而 <climits> 存在于 C++ 语言中。在这些头文件中定义了多个宏。这些限制指定变量不能存储超出这些限制的值。

“limits.h”或 <climits> 头文件中的某些宏如下

CHAR_BITLONG_MINLONG_MAXCHAR_MINCHAR_MAX
INT_MININT_MAXSHRT_MINSHRT_MAXULONG_MAX

以下是 C++ 语言中 <climits> 的一个示例

示例

 在线演示

#include<iostream>
#include <bits/stdc++.h>
#include<climits>
using namespace std;
int main() {
   int x = 28;
   int a = CHAR_BIT*sizeof(x);
   stack<bool> s;
   cout << "The number is : " << x << endl;
   for (int i=1; i<=a; i++) {
      s.push(x%2);
      x = x/2;
   }
   cout << "The number of bits in a byte : " << CHAR_BIT << endl;
   for (int i=1; i<=a; i++) {
      cout << s.top();
      s.pop();
      if (i % CHAR_BIT == 0)
      cout << " ";
   }
   cout << "\n\nThe minimum value of short int :" << SHRT_MIN;
   return 0;
}

输出

The number is : 28
The number of bits in a byte : 8
00000000 00000000 00000000 00011100

The minimum value of short int :-32768

在上述程序中,表示了 <climits> 的两个宏。一个是 CHAR_BIT,另一个是 SHRT_MIN。CHAR_BIT 定义了 char 中的位数,SHRT_MIN 定义了 short int 的最小值。

int x = 28;
int a = CHAR_BIT*sizeof(x);
….
cout << "\n\nThe minimum value of short int :" << SHRT_MIN;

更新于: 26-6 月 - 2020

1K+ 浏览量

开启您的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.