Introduction

The limits.h header determines various properties of the various variable types. The macros defined in this header limits the values of various variable types like char, int and long.

These limits specify that a variable can not store any value beyond these limits, for example an unsigned character can store upto a maximum value of 255.

Library Macros

The following values are implementation-specific and defined with the #define directive, but these values may not be any lower than what is given here.

Macro Value Description
CHAR_BIT 8 Defines the number of bits in a byte.
SCHAR_MIN -128 Defines the minimum value for a signed char.
SCHAR_MAX 127 Defines the maximum value for a signed char.
UCHAR_MAX 255 Defines the maximum value for an unsigned char.
CHAR_MIN 0 Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values otherwise zero.
CHAR_MAX 127 Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values otherwise UCHAR_MAX
MB_LEN_MAX 1 Defines the maximum number of bytes in a multibyte character.
SHRT_MIN -32768 Defines the minimum value for a short int.
SHRT_MAX +32767 Defines the maximum value for a short int.
USHRT_MAX 65535 Defines the maximum value for an unsigned short int.
INT_MIN -32768 Defines the minimum value for an int.
INT_MAX +32767 Defines the maximum value for an int.
UINT_MAX 65535 Defines the maximum value for an unsigned int.
LONG_MIN -2147483648 Defines the minimum value for a long int.
LONG_MAX +2147483647 Defines the maximum value for a long int.
ULONG_MAX 4294967295 Defines the maximum value for an unsigned long int.

Example

The following example shows the usage of few of the constants defined in limit.h file.

#include <stdio.h>
#include <limits.h> int main()
{ printf("The number of bits in a byte %d\n", CHAR_BIT); printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX); printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
printf("The maximum value of SHORT INT = %d\n", SHRT_MAX); printf("The minimum value of INT = %d\n", INT_MIN);
printf("The maximum value of INT = %d\n", INT_MAX); printf("The minimum value of CHAR = %d\n", CHAR_MIN);
printf("The maximum value of CHAR = %d\n", CHAR_MAX); printf("The minimum value of LONG = %ld\n", LONG_MIN);
printf("The maximum value of LONG = %ld\n", LONG_MAX); return(0);
}

Let us compile and run the above program, this will produce the following result:

The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -32768
The maximum value of INT = 32767
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -2147483648
The maximum value of LONG = 2147483647

C Library - <limits.h>的更多相关文章

  1. C 标准库系列之limits.h

    limits.h 类似于float.h,其定义了一些列类型的值的范围.限制值,也就是说该类型的值不可以大于该上界或者小于该类型值的下界: glibc中宏定义了一下的值: # define MB_LEN ...

  2. limits.h头文件

    CHAR,SHRT,INT ,LLONG加_MAX后缀表示最大,加_MIN后缀表示最小,加U前缀表示无符号 UCHAR_MIN ,UCHAR_MAX sizeof()计算数所用的空间 #include ...

  3. <limits.h>和<float.h>

    头文件<limits.h>中定义了用于表示整类型大小的常量.以下所列的值是可接受的最小值,实际系统中可能有更大的值. CHAR_BIT char类型的位数 CHAR_MAX UCHAR_M ...

  4. LIMITS.H

    /*--------------------------------------------------------------------------LIMITS.H ANSI standard i ...

  5. 如何解决编译linux内核(解决声卡问题),遭遇fatal error: linux/limits.h: 没有那个文件或目录

    最近帮一位上海的朋友搞一块小板,在ubuntu15.04 vivid上已经加载了对应了.ko驱动包 但关键是系统根本就枚举不到该声卡ALC5640,试了OpenSUSE也是一样的结果,看来是内核漏加载 ...

  6. #include <limits.h>

    limits.h专门用于检测整型数据数据类型的表达值范围. #include<stdio.h> #include<limits.h> main() { printf(" ...

  7. [Perl]Can't link/include C library 'ft2build.h', 'freetype', aborting.

    原文:http://www.code-by.org/viewtopic.php?f=60&t=284 错误提示 Font-FreeType-0.07>perl Makefile.PL B ...

  8. VC++中对数据类型的限制limits.h文件内容

    limits.h文件中规定了是IDE在OS中规定了每个数据类型的最大值和最小值以及在程序源代码中编译时候所占用的字节数,这这样做有利于帮助程序员在编写程序的时候有效控制在选择合适数据类型的显示范围值. ...

  9. C 标准库 - <limits.h>

    C 标准库 - <limits.h> 简介 limits.h 头文件决定了各种变量类型的各种属性.定义在该头文件中的宏限制了各种变量类型(比如 char.int 和 long)的值. 这些 ...

随机推荐

  1. 洛谷 P2048 [NOI2010]超级钢琴(优先队列,RMQ)

    传送门 我们定义$(p,l,r)=max\{sum[t]-sum[p-1],p+l-1\leq t\leq p+r-1 \}$ 那么因为对每一个$p$来说$sum[p-1]$是一个定值,所以我们只要在 ...

  2. jquery 插件的实现和优化

    1.menus 实现: $.fn.menu=function(options){ var $this=$(this); var cross='<div class="zhiniu_cr ...

  3. Java基础--面向对象以及相关知识

    一. 面向对象特征与原则 1.三大特征:封装.继承.多态. (1)封装:将客观的事物封装成抽象的类,封装代码逻辑,并通过访问控制符来控制访问的方式,从而保护程序(使用反射时,可以获取对象的私有方法和成 ...

  4. 分布式集群环境下,如何实现session共享一(应用场景)

    在web应用中,由于http的请求响应式,无状态.要记录用户相关的状态信息,比如电商网站的购物车,比如用户是否登录等,都需要使用session.我们知道session是由servlet容器创建和管理, ...

  5. java模拟多线程

    public class HTTPRequest  implements Runnable { public void run() { //这里实现发送请求 } public static void ...

  6. css各种水平垂直居中

    css各种水平垂直居中,网上查了一下,总结以下几种 line-height垂直居中 缺点,仅限于单行文字 .item{ text-align: center; height: 100px; line- ...

  7. GUI的最终选择 Tkinter(七):菜单Menu组件、Menubutton组件、OptionMenu组件

    Menu组件 今天说的Menu组件就是一些菜单组件,开始点点点... Tkinter提供了一个Menu组件,可以实现顶级菜单,下拉菜单和弹出菜单.由于底层是代码实现和优化的,所以不太建议通过按钮和其他 ...

  8. F. Coprime Subsequences 莫比乌斯反演

    http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...

  9. @Primary 使用

    造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...

  10. 常见的JedisConnectionException 异常

    最近在使用redis出现以下的异常: 1.redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectExcept ...