如何在c中找到所有数据类型的限制 [英] how to find the limits of all data types in c

查看:27
本文介绍了如何在c中找到所有数据类型的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道我们在c中使用的所有数据类型都有固定限制

We all know that all the data types which we used in c has fix limit like

char range from -128 to 127
integer store value from -2147483648 to 2147483647

像这样都有固定限制.

我的问题很简单,如果我们使用头文件,我们可以很容易地找出所有数据类型的范围.但是,是否有任何流程或逻辑可以让我们在不使用任何预定义函数或头文件或宏的情况下找到所有这些数据类型的限制??

My question is very simple that if we use header file we can easily find out the range of all data types. But is there any process or logic through which we can find limits of all these data types without using Any predefined function or header files or Macros??

如果有请解决我的问题

我也想知道头文件.有没有什么方法可以改变浮点数中小数点后的位数??

and I also want to know about header file. Is there any method to vary the number of digit after decimal in float ??

推荐答案

我对 C 很陌生,但我注意到如果你减少一个无符号类型,它将被分配最大值.

I'm pretty new to C but I noticed that if you decrement an unsigned type it will be assigned the maximum value.

我包含了 limits.h 只是为了比较结果.

I've included limits.h just to compare the results.

#include <stdio.h>
#include <limits.h>

int main(void)
{   
    unsigned char c;
    unsigned short s;
    unsigned int i;
    unsigned long l;

    // char
    printf("UCHAR_MAX  = %u\n",  UCHAR_MAX);
    printf("UCHAR_MAX  = %u\n", --c);

    // short
    printf("USHRT_MAX  = %u\n",  USHRT_MAX);
    printf("USHRT_MAX  = %u\n",  --s);

    // int
    printf("UINT_MAX   = %u\n",  UINT_MAX);
    printf("UINT_MAX   = %u\n",  --i);

    // long
    printf("ULONG_MAX  = %lu\n",  ULONG_MAX);
    printf("ULONG_MAX  = %lu\n",  --l);
}   

结果:

UCHAR_MAX  = 255
UCHAR_MAX  = 255
USHRT_MAX  = 65535
USHRT_MAX  = 65535
UINT_MAX   = 4294967295
UINT_MAX   = 4294967295
ULONG_MAX  = 18446744073709551615
ULONG_MAX  = 140731079060271

正如你所看到的,除了长时间之外,它对所有人都有效,至于为什么......好吧,也许有人比我更有经验可以解释.

As you can see it works for all except long, as to why.. well maybe someone more experienced than me can explain.

这篇关于如何在c中找到所有数据类型的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆