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

由limits.h看整型范圍

系統(tǒng) 2258 0

歡迎訪問我的新博客: http://www.milkcu.com/blog/

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

前言

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

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

編寫一個程序以確定分別由signed及unsigned限定的char、short、int與long類型變量的取值范圍。采用打印標準頭文件中的相應(yīng)值以及直接計算兩種方式實現(xiàn)。后一種方式的實現(xiàn)較困難一些,因為要確定各種浮點類型的取值范圍。

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

實現(xiàn)方案

標準規(guī)定:各種類型的取值范圍必須在頭文件<limits.h>中定義。不同類型在不同的硬件上有不同的長度,所以它們在不同機器上的取值范圍也往往會不同。我們可以通過頭文件確定類型取值范圍。

函數(shù)sizeof()

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

    # 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;
}
  

打印常量

可以通過打印頭文件的方法獲得每種類型的取值范圍:

    # 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>中更詳細的看到它們的宏定義,<limits.h>(來自 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ù)類型的定義。

總結(jié)

簡而言之,對于MinGW32:

int類型,能完整表示9位;
unsigned long long int類型,能完整表示19位。

后記

又一輪新生活開始了,要充實快樂每一天。

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

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

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

(全文完)

由limits.h看整型范圍


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91成人免费福利网站在线 | 99国产在线 | 日本精品视频在线 | 国产精品午夜久久 | 99这里精品 | 奇米影视奇米四色888av | 色综合久久伊人 | 四虎影院免费在线播放 | 精品无码久久久久久久动漫 | 全部在线播放免费毛片 | 成人三级毛片 | 51国产午夜精品免费视频 | 99热在线精品观看 | 日日摸夜夜夜夜夜添 | 福利影院在线看 | 91精品国产免费久久 | 欧美αv在线 | 午夜综合| 国产凹凸在线一区二区色老头 | 中文精品视频一区二区在线观看 | 97影院不用 | 波多野结衣亚洲一区二区三区 | 婷婷亚洲视频 | 国偷盗摄自产福利一区在线 | 伊人成影院九九 | 最新四虎4hu影库地址在线 | 日本特级 | 天天干视频| 播五月综合 | 天天干天天射天天操 | 婷婷亚洲国产成人精品性色 | 日本特黄特色aaa大片免费 | 国产成人aa免费视频 | 97se亚洲国产综合自在线观看 | 中文字幕亚洲综合 | 成年免费网站 | 色综合亚洲综合网站综合色 | 五月婷婷综合色 | 免费看人做人爱视频拍拍拍 | 亚洲精品一区二 | 国产精品免费大片一区二区 |