(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.