range on int, short and other data types in C on m

2019-05-13 11:50发布

I was reading a old C book and according to that

int range values from -32768 to +32767.

where as my machine can hold a larger int than that limit how can I find the range of these data types(short, int, long, double, float) specific to my machine?

are there any methods for that?

3条回答
Juvenile、少年°
2楼-- · 2019-05-13 12:30

From this, take a look at the footnote:

  • the actual value depends on the particular system and library implementation, but shall reflect the limits of these types in the target platform.

If you run the following code on your system, it should shed some insight because the value returned may or may not differ from the ones in the above link.

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

    int main() {
        printf("\t\tUsing <limits.h> library definitions...\n");
        printf("CHAR\n");
        printf("signed char max: %d\n", SCHAR_MAX);
        printf("unsigned char max: %u\n", UCHAR_MAX); // Note use of u, formatting output
        printf("signed char min: %d\n", SCHAR_MIN);
        printf("SHORT\n");
        printf("signed short min: %d\n", SHRT_MIN);
        printf("signed short max: %d\n", SHRT_MAX);
        printf("unsigned short max: %d\n", USHRT_MAX);
        printf("INT\n");
        printf("signed int max: %d\n", INT_MAX);
        printf("unsigned int max: %u\n", UINT_MAX); 
        printf("signed int min: %d\n", INT_MIN);
        printf("LONG\n");
        printf("signed long max: %d\n", LONG_MAX);
        printf("unsigned long max: %u\n", ULONG_MAX);
        printf("signed long min: %d\n", LONG_MIN);
        printf("FLOAT\n");
        printf("signed float max: %e\n", FLT_MAX);
        printf("signed float min: %e\n", FLT_MIN);
        printf("DOUBLE\n");
        printf("signed double max: %e\n", DBL_MAX);
        printf("signed double min: %e\n", DBL_MIN);
        return 0;
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-13 12:35

I hope I understood your qestion correctly.

You can take a look at limits.h, where you can find the sizes of integral types. Take a look here.

This header defines constants with the limits of fundamental integral types for the specific system and compiler implementation used.

查看更多
孤傲高冷的网名
4楼-- · 2019-05-13 12:37

Header <limits.h> provides these informations while <stdint.h> allows you to specify specific width integers:

INT_MAX
INT_MIN
int32_t value; // to have an integer of exactly 32 bits
查看更多
登录 后发表回答