ค่าสูงสุดและต่ำสุดของแต่ละตัวแปร integer ในภาษา C

เราสามารถหาค่าสูงสุดและต่ำสุดของตัวแปร ตัวเลขจำนวนเต็ม (Integer) แต่ละชนิดในภาษา C ได้จากค่า Define ที่อยู่ใน ไฟล์ limit.h
ตารางด้านล่างนี้แสดง Define ที่ใช้สำหรับกำหนดค่าสูงสุด (Max) หรือต่ำสุด (Min)

ซึ่งสามารถนำเอาค่า Define มา print แสดงค่าได้ โดย Include ไฟล์ limit.h ดัง Source code ตัวอย่าง

#include <stdio.h>
#include <limits.h> /* For limits on integer types */

int main(void)
{
    //unsigned char
    printf("min unsigned char = 0\n");
    printf("max unsigned char max = %u\n",UCHAR_MAX);
    printf("size unsigned char = %d\n\n", sizeof(unsigned char));
     //char
    printf("min char = %d\n",CHAR_MIN);
    printf("max char = %d\n", CHAR_MAX);
    printf("size char = %d byte\n\n\n",sizeof(char));

    //unsigned int
    printf("min unsigned int = 0\n");
    printf("max unsigned int  = %u\n", UINT_MAX);
    printf("size unsigned int  = %d\n\n", sizeof(unsigned int));
    //int
    printf("min int = %d\n", INT_MIN);
    printf("max int = %d\n", INT_MAX);
    printf("size int = %d byte\n\n\n", sizeof(int));


    //unsigned short
    printf("min unsigned short = 0\n");
    printf("max unsigned short = %u\n", USHRT_MAX);
    printf("size unsigned short = %d\n\n", sizeof(unsigned short));
    //short
    printf("min short = %d\n",SHRT_MIN);
    printf("max short = %d\n",SHRT_MAX);
    printf("size short = %d byte\n\n\n",sizeof(short));
    

    //unsigned long
    printf("min unsigned long = 0\n");
    printf("max unsigned long =  %lu\n",ULONG_MAX);
    printf("size unsigned long = %d\n\n",sizeof(unsigned long));
    //long
    printf("min long = %ld\n",LONG_MIN);
    printf("max long = %ld\n",LONG_MAX);
    printf("size long = %d byte\n\n\n", sizeof(long));


    //unsigned logn long
    printf("min unsigned long long = 0\n");
    printf("max unsigned long long = %llu\n", ULLONG_MAX);
    printf("min unsigned long long = %d\n\n", sizeof(unsigned long long));
    //logn long
    printf("min long long = %lld\n", LLONG_MIN);
    printf("max long long = %lld\n", LLONG_MAX);
    printf("size long long = %d byte\n\n\n", sizeof(long long));
    
    return 0;
}

จะได้ผลลัพธ์ดังนี้ (Windows 7 32 bit, gcc 4.9.2)

min unsigned char = 0
max unsigned char max = 255
size unsigned char = 1

min char = -128
max char = 127
size char = 1 byte

min unsigned int = 0
max unsigned int = 4294967295
size unsigned int = 4

min int = -2147483648
max int = 2147483647
size int = 4 byte

min unsigned short = 0
max unsigned short = 65535
size unsigned short = 2

min short = -32768
max short = 32767
size short = 2 byte

min unsigned long = 0
max unsigned long = 4294967295
size unsigned long = 4

min long = -2147483648
max long = 2147483647
size long = 4 byte

min unsigned long long = 0
max unsigned long long = 18446744073709551615
min unsigned long long = 8

min long long = -9223372036854775808
max long long = 9223372036854775807
size long long = 8 byte