亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

由limits.h看整型范圍

系統(tǒng) 2623 0

歡迎訪問(wèn)我的新博客: http://www.milkcu.com/blog/

原文地址: http://www.milkcu.com/blog/archives/1367305920.html

前言

聲明一個(gè)變量,經(jīng)常要考慮的問(wèn)題是這個(gè)類(lèi)型的變量能不能裝的下。今天 MilkCu 就總結(jié)下吧,以解除后顧之憂。

關(guān)于變量取值范圍的問(wèn)題,在Kernighan的《C程序設(shè)計(jì)語(yǔ)言》第28頁(yè)練習(xí)2-1就提到過(guò)。

編寫(xiě)一個(gè)程序以確定分別由signed及unsigned限定的char、short、int與long類(lèi)型變量的取值范圍。采用打印標(biāo)準(zhǔn)頭文件中的相應(yīng)值以及直接計(jì)算兩種方式實(shí)現(xiàn)。后一種方式的實(shí)現(xiàn)較困難一些,因?yàn)橐_定各種浮點(diǎn)類(lèi)型的取值范圍。

這書(shū)是針對(duì)C89的,隨著C99及C++的廣泛應(yīng)用long long int類(lèi)型也是一種美妙的選擇。

實(shí)現(xiàn)方案

標(biāo)準(zhǔn)規(guī)定:各種類(lèi)型的取值范圍必須在頭文件<limits.h>中定義。不同類(lèi)型在不同的硬件上有不同的長(zhǎng)度,所以它們?cè)诓煌瑱C(jī)器上的取值范圍也往往會(huì)不同。我們可以通過(guò)頭文件確定類(lèi)型取值范圍。

函數(shù)sizeof()

可以通過(guò)函數(shù)sizeof()用字節(jié)計(jì)算參數(shù)并返回的字節(jié)數(shù),間接得到類(lèi)型寬度,源代碼如下:

    # include <stdio.h>
int main(void)
{
	printf("byte of short = %d\n", sizeof(short));
	printf("byte of int = %d\n", sizeof(int));
	printf("byte of long = %d\n", sizeof(long));
	printf("byte of long long int = %d\n", sizeof(long long int));
	return 0;
}
  

打印常量

可以通過(guò)打印頭文件的方法獲得每種類(lèi)型的取值范圍:

    # include <stdio.h>
# include <limits.h>
//determine ranges of types
int main(void)
{
	//signed types
	printf("signed char min = %d\n", SCHAR_MIN);
	printf("signed char max = %d\n", SCHAR_MAX);
	printf("signed short min = %d\n", SHRT_MIN);
	printf("signed short max = %d\n", SHRT_MIN);
	printf("signed short min = %d\n", INT_MIN);
	printf("signed long min = %ld\n", LONG_MIN);
	printf("signed long max = %ld\n", LONG_MAX);
	printf("signed long long int min = %lld\n", LONG_LONG_MIN);
	printf("signed long long int max = %lld\n", LONG_LONG_MAX);
	//unsigned types
	printf("unsigned char max = %u\n", UCHAR_MAX);
	printf("unsigned short max = %u\n", USHRT_MAX);
	printf("unsigned int max = %u\n", UINT_MAX);
	printf("unsigned long max = %lu\n", ULONG_MAX);
	printf("unsigned long long int max = %llu\n", ULONG_LONG_MAX);
	return 0;
}
  

打印結(jié)果如下:

    signed char min = -128
signed char max = 127
signed short min = -32768
signed short max = -32768
signed short min = -2147483648
signed long min = -2147483648
signed long max = 2147483647
signed long long int min = -9223372036854775808
signed long long int max = 9223372036854775807
unsigned char max = 255
unsigned short max = 65535
unsigned int max = 4294967295
unsigned long max = 4294967295
unsigned long long int max = 18446744073709551615
  

頭文件

可以從頭文件<limits.h>中更詳細(xì)的看到它們的宏定義,<limits.h>(來(lái)自 Dev-Cpp 5.4.0 MinGW 4.7.2 )如下:

    /* 
 * limits.h
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 *
 * Functions for manipulating paths and directories (included from io.h)
 * plus functions for setting the current drive.
 *
 * Defines constants for the sizes of integral types.
 *
 * NOTE: GCC should supply a version of this header and it should be safe to
 *       use that version instead of this one (maybe safer).
 *
 */

#ifndef _LIMITS_H_
#define _LIMITS_H_

/* All the headers include this file. */
#include <_mingw.h>

/*
 * File system limits
 *
 * TODO: NAME_MAX and OPEN_MAX are file system limits or not? Are they the
 *       same as FILENAME_MAX and FOPEN_MAX from stdio.h?
 * NOTE: PATH_MAX is the POSIX equivalent for Microsoft's MAX_PATH; the two
 *       are semantically identical, with a limit of 259 characters for the
 *       path name, plus one for a terminating NUL, for a total of 260.
 */
#define PATH_MAX	260

/*
 * Characteristics of the char data type.
 *
 * TODO: Is MB_LEN_MAX correct?
 */
#define CHAR_BIT	8
#define MB_LEN_MAX	2

#define SCHAR_MIN	(-128)
#define SCHAR_MAX	127

#define UCHAR_MAX	255

/* TODO: Is this safe? I think it might just be testing the preprocessor,
 *       not the compiler itself... */
#if	('\x80' < 0)
#define CHAR_MIN	SCHAR_MIN
#define CHAR_MAX	SCHAR_MAX
#else
#define CHAR_MIN	0
#define CHAR_MAX	UCHAR_MAX
#endif

/*
 * Maximum and minimum values for ints.
 */
#define INT_MAX		2147483647
#define INT_MIN		(-INT_MAX-1)

#define UINT_MAX	0xffffffff

/*
 * Maximum and minimum values for shorts.
 */
#define SHRT_MAX	32767
#define SHRT_MIN	(-SHRT_MAX-1)

#define USHRT_MAX	0xffff

/*
 * Maximum and minimum values for longs and unsigned longs.
 *
 * TODO: This is not correct for Alphas, which have 64 bit longs.
 */
#define LONG_MAX	2147483647L
#define LONG_MIN	(-LONG_MAX-1)

#define ULONG_MAX	0xffffffffUL

#ifndef __STRICT_ANSI__
/* POSIX wants this.  */ 
#define SSIZE_MAX LONG_MAX
#endif

#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \
     || !defined(__STRICT_ANSI__)
/* ISO C9x macro names */
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (2ULL * LLONG_MAX + 1)
#endif

/*
 * The GNU C compiler also allows 'long long int'
 */
#if !defined(__STRICT_ANSI__) && defined(__GNUC__)

#define LONG_LONG_MAX	9223372036854775807LL
#define LONG_LONG_MIN	(-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX	(2ULL * LONG_LONG_MAX + 1)

/* MSVC compatibility */
#define _I64_MIN LONG_LONG_MIN
#define _I64_MAX LONG_LONG_MAX
#define _UI64_MAX ULONG_LONG_MAX

#endif /* Not Strict ANSI and GNU C compiler */


#endif /* not _LIMITS_H_ */
  

閱讀頭文件,在注釋的幫助下,我們可以更明確的看到數(shù)據(jù)類(lèi)型的定義。

總結(jié)

簡(jiǎn)而言之,對(duì)于MinGW32:

int類(lèi)型,能完整表示9位;
unsigned long long int類(lèi)型,能完整表示19位。

后記

又一輪新生活開(kāi)始了,要充實(shí)快樂(lè)每一天。

借用奧斯托洛夫斯基在 《鋼鐵是怎樣煉成》 中的幾句話吧:

人最寶貴的東西是生命
生命屬于人只有一次
一個(gè)人的生命是應(yīng)該這樣度過(guò)的
當(dāng)他回首往事的時(shí)候
他不會(huì)因虛度年華而悔恨
也不會(huì)因碌碌無(wú)為而羞恥

這樣在臨死的時(shí)候
他才能夠說(shuō):“我的生命和全部的經(jīng)歷
都獻(xiàn)給世界上最壯麗的事業(yè)——為人類(lèi)的解放而斗爭(zhēng)”。

(全文完)

由limits.h看整型范圍


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 一区二区三区精品国产欧美 | 99免费在线观看视频 | 97国产在线播放 | 国产精品久久久久久亚洲伦理 | 国产亚洲精品久久久久久久网站 | 热久久久| 成人小视频免费在线观看 | 亚欧洲精品在线视频免费观看 | 国产三级在线精品男人的天堂 | 久青草视频在线播放 | 婷婷毛片 | 九九精品久久 | 亚洲精品二三区伊人久久 | 小视频在线免费观看 | 久久99精品亚洲热综合 | 亚洲精品永久一区 | 五月婷婷在线观看视频 | www.男人天堂.com| 欧美亚洲国产日韩综合在线播放 | 777777农村一级毛片 | 四虎影永久在线观看网址 | 久夜色精品国产一区二区三区 | 中文字幕日本不卡一二三区 | 午夜影院毛片 | 免费区欧美一级毛片精品 | 国产九九热视频 | 亚洲天天操 | 欧美日韩国产高清精卡 | 四虎影院视频在线观看 | 亚洲 国产 日韩 欧美 | 99在线热视频 | 国产精品区一区二区免费 | 久久精品免视看国产成人2021 | 国产成人在线免费视频 | 一级做受毛片免费大片 | 久操久热 | 欧美另类综合 | 中文字幕在线观看2023 | 欧美视频在线观看一区二区 | 亚洲高清国产一区二区三区 | 日日操日日碰 |