tft每日頭條

 > 科技

 > c語言最大值和最小值

c語言最大值和最小值

科技 更新时间:2024-10-04 00:33:08

c語言最大值和最小值(基本數據類型的大小和極值範圍)1

C 提供了豐富的内置數據類型,用戶也可以自定義數據類型,下面是5種基本數據類型:

  • 布爾型:bool
  • 字符型:char
  • 整型:int
  • 浮點型:float
  • 雙浮點型:double

基本數據類型還可以使用下面的類型修飾符進行修飾:

  • 有符号:signed
  • 無符号:unsigned
  • 短整型:short
  • 長整型:long

int 是基本的整數類型,默認是有符号的(signed ),unsigned 表示無符号,無符号值可以避免誤存負數, 同時擴大了正數的表示範圍。

short 和 long 是在 int 的基礎上進行的擴展,使用 short int 可以節省内存,long int 則可以表示更大的值。

1、數據類型的大小

sizeof() 是一個判斷數據類型或者表達式長度的運算符,以字節為單位。

C語言運算符sizeof的用法

c語言最大值和最小值(基本數據類型的大小和極值範圍)2

2、數據類型的範圍

一般數值類型的最小值和最大值與平台相關,C 11中通過模闆類 std::numeric_limits,提供了基礎算術類型的極值等屬性信息,用于取代<climits> 和 <limits.h>,浮點常數定義于 <cfloat> 和 <float.h>。

template <class T> numeric_limits;

The numeric_limits class template provides a standardized way to query various properties of arithmetic types.

代碼如下:

#include <iostream> using namespace std; int main() { cout << "sizeof(bool) : " << sizeof(bool) << endl; cout << "sizeof(char) : " << sizeof(char) << endl; cout << "sizeof(int) : " << sizeof(int) << endl; cout << "sizeof(unsigned int) : " << sizeof(unsigned int) << endl; cout << "sizeof(short int) : " << sizeof(short int) << endl; cout << "sizeof(long int) : " << sizeof(long int) << endl; cout << "sizeof(float) : " << sizeof(float) << endl; cout << "sizeof(double) : " << sizeof(double) << endl; cout << "min(bool) : " << numeric_limits<bool>::min() << endl; cout << "min(int) : " << numeric_limits<int>::min() << endl; cout << "min(unsigned int) : " << numeric_limits<unsigned int>::min() << endl; cout << "min(short int) : " << numeric_limits<short int>::min() << endl; cout << "min(long int) : " << numeric_limits<long int>::min() << endl; cout << "min(float) : " << numeric_limits<float>::min() << endl; cout << "min(double) : " << numeric_limits<double>::min() << endl; cout << "max(bool) : " << numeric_limits<bool>::max() << endl; cout << "max(int) : " << numeric_limits<int>::max() << endl; cout << "max(unsigned int) : " << numeric_limits<unsigned int>::max() << endl; cout << "max(short int) : " << numeric_limits<short int>::max() << endl; cout << "max(long int) : " << numeric_limits<long int>::max() << endl; cout << "max(float) : " << numeric_limits<float>::max() << endl; cout << "max(double) : " << numeric_limits<double>::max() << endl; return 0; }

運行結果如下:

c語言最大值和最小值(基本數據類型的大小和極值範圍)3

C 基本數據類型的大小和極值範圍,總結如下表所示:

c語言最大值和最小值(基本數據類型的大小和極值範圍)2

相關閱讀

初識C語言指針

深入理解C語言的指針

C 中指針與引用的區别

C 的友元函數和友元類

C 類的三種繼承方式:public/protected/private

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved