(limits.h) 在 C/C++ 中
头文件“limits.h”存在于 C 语言中,而 <climits> 存在于 C++ 语言中。在这些头文件中定义了多个宏。这些限制指定变量不能存储超出这些限制的值。
“limits.h”或 <climits> 头文件中的某些宏如下
| CHAR_BIT | LONG_MIN | LONG_MAX | CHAR_MIN | CHAR_MAX |
| INT_MIN | INT_MAX | SHRT_MIN | SHRT_MAX | ULONG_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;
广告
数据结构
网络
关系型 DBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP