Setiap tipe data di C memiliki ukuran dan rentang nilai yang spesifik—char selalu 1 byte, int biasanya 4 byte pada CPU modern, dan long bisa 4 byte atau 8 byte tergantung model data (LP64 di Linux vs LLP64 di Windows). Memahami rentang setiap tipe membantu kita memilih tipe yang tepat untuk menyimpan nilai yang besar, menghindari overflow, dan menulis kode portable. Bab ini menyediakan tabel lengkap ukuran dan rentang beserta macro standar dari <limits.h>, <float.h>, dan <stdint.h>.
Tipe Integer Dasar #
Tabel berikut mencantumkan tipe integer dasar C beserta ukuran umum pada platform LP64 (Linux x86_64). Ukuran sebenarnya dapat berbeda pada platform lain—hanya char yang dijamin tepat 1 byte oleh standar.
| Tipe | Ukuran | Signed min | Signed max | Unsigned max |
|---|---|---|---|---|
char | 1 byte | -128 | 127 | 255 |
signed char | 1 byte | -128 | 127 | — |
unsigned char | 1 byte | — | — | 255 |
short | 2 byte | -32.768 | 32.767 | 65.535 |
unsigned short | 2 byte | — | — | 65.535 |
int | 4 byte | -2.147.483.648 | 2.147.483.647 | 4.294.967.295 |
unsigned int | 4 byte | — | — | 4.294.967.295 |
long | 8 byte (LP64) / 4 byte (LLP64) | -9.2 × 10^18 | 9.2 × 10^18 | 1.8 × 10^19 |
unsigned long | sama dengan long | — | — | 1.8 × 10^19 |
long long | 8 byte (minimal) | -9.2 × 10^18 | 9.2 × 10^18 | 1.8 × 10^19 |
unsigned long long | 8 byte | — | — | 1.8 × 10^19 |
Macro Standar <limits.h> #
Standar C menjamin macro berikut tersedia di <limits.h> untuk mendapatkan rentang tipe integer. Macro ini memberi tahu kita rentang aktual pada platform tempat kode di-compile, sehingga kita tidak perlu menebak.
| Macro | Nilai (x86_64) | Deskripsi |
|---|---|---|
CHAR_BIT | 8 | Jumlah bit dalam 1 byte |
SCHAR_MIN | -128 | signed char minimum |
SCHAR_MAX | 127 | signed char maksimum |
UCHAR_MAX | 255 | unsigned char maksimum |
SHRT_MIN | -32768 | short minimum |
SHRT_MAX | 32767 | short maksimum |
USHRT_MAX | 65535 | unsigned short maksimum |
INT_MIN | -2147483648 | int minimum |
INT_MAX | 2147483647 | int maksimum |
UINT_MAX | 4294967295 | unsigned int maksimum |
LONG_MIN | -9223372036854775808 | long minimum |
LONG_MAX | 9223372036854775807 | long maksimum |
ULONG_MAX | 18446744073709551615 | unsigned long maksimum |
LLONG_MIN | -9223372036854775808 | long long minimum |
LLONG_MAX | 9223372036854775807 | long long maksimum |
ULLONG_MAX | 18446744073709551615 | unsigned long long maksimum |
Tipe Floating Point #
Tipe floating point di C mengikuti standar IEEE-754. Tabel berikut mencantumkan rentang dan presisi yang umum pada platform x86_64.
| Tipe | Ukuran | Eksponen min | Eksponen max | Approx range | Digit presisi |
|---|---|---|---|---|---|
float | 4 byte | -38 | +38 | ±1.2 × 10^-38 s/d ±3.4 × 10^38 | 6-7 |
double | 8 byte | -308 | +308 | ±2.2 × 10^-308 s/d ±1.8 × 10^308 | 15-16 |
long double | 10-16 byte | -4932 | +4932 | ±3.4 × 10^-4932 s/d ±1.2 × 10^4932 | 18-19 |
Macro di <float.h>:
| Macro | Nilai (x86_64) | Deskripsi |
|---|---|---|
FLT_MIN | 1.175494e-38 | float positif terkecil (normalized) |
FLT_MAX | 3.402823e+38 | float positif terbesar |
FLT_EPSILON | 1.192093e-07 | Selisih terkecil yang dapat dibedakan |
FLT_DIG | 6 | Jumlah digit desimal presisi |
FLT_MANT_DIG | 24 | Jumlah bit mantissa |
DBL_MIN | 2.225074e-308 | double positif terkecil |
DBL_MAX | 1.797693e+308 | double positif terbesar |
DBL_EPSILON | 2.220446e-16 | Selisih terkecil yang dapat dibedakan |
DBL_DIG | 15 | Jumlah digit desimal presisi |
LDBL_DIG | 18 | long double presisi |
Tipe Exact-Width <stdint.h> #
Header <stdint.h> (sejak C99) menyediakan tipe integer dengan ukuran pasti yang tidak bergantung pada platform. Kita sebaiknya memakai tipe ini untuk format file, protokol jaringan, atau API yang mensyaratkan ukuran tertentu.
| Tipe | Ukuran | Min | Max |
|---|---|---|---|
int8_t | 8 bit | -128 | 127 |
uint8_t | 8 bit | 0 | 255 |
int16_t | 16 bit | -32.768 | 32.767 |
uint16_t | 16 bit | 0 | 65.535 |
int32_t | 32 bit | -2.147.483.648 | 2.147.483.647 |
uint32_t | 32 bit | 0 | 4.294.967.295 |
int64_t | 64 bit | -9.223.372.036.854.775.808 | 9.223.372.036.854.775.807 |
uint64_t | 64 bit | 0 | 18.446.744.073.709.551.615 |
int_least8_t | ≥8 bit | — | — |
int_fast8_t | ≥8 bit (fast) | — | — |
intptr_t | pointer-sized | — | — |
uintptr_t | pointer-sized unsigned | — | — |
intmax_t | lebar maksimum | — | — |
uintmax_t | lebar maksimum unsigned | — | — |
Macro pasangan untuk rentang: INT8_MIN, INT8_MAX, UINT8_MAX, …, INT64_MAX, UINT64_MAX.
Program Lengkap #
Program berikut mencetak ukuran dan rentang semua tipe dasar dan tipe stdint.h. Output berasal dari GCC 14.2 pada Linux x86_64.
/* File: tipe1.c - Ukuran dan rentang tipe data */
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <stdint.h>
#include <stddef.h>
#include <inttypes.h>
int main(void) {
printf("== Ukuran tipe dasar ==\n");
printf("sizeof(char) = %zu byte\n", sizeof(char));
printf("sizeof(short) = %zu byte\n", sizeof(short));
printf("sizeof(int) = %zu byte\n", sizeof(int));
printf("sizeof(long) = %zu byte\n", sizeof(long));
printf("sizeof(long long) = %zu byte\n", sizeof(long long));
printf("sizeof(float) = %zu byte\n", sizeof(float));
printf("sizeof(double) = %zu byte\n", sizeof(double));
printf("sizeof(long double) = %zu byte\n", sizeof(long double));
printf("sizeof(void*) = %zu byte\n", sizeof(void*));
printf("sizeof(size_t) = %zu byte\n", sizeof(size_t));
printf("\n== Rentang signed integer ==\n");
printf("INT_MIN = %d\n", INT_MIN);
printf("INT_MAX = %d\n", INT_MAX);
printf("LONG_MIN = %ld\n", LONG_MIN);
printf("LONG_MAX = %ld\n", LONG_MAX);
printf("LLONG_MAX = %lld\n", LLONG_MAX);
printf("\n== Rentang unsigned integer ==\n");
printf("UINT_MAX = %u\n", UINT_MAX);
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("ULLONG_MAX = %llu\n", ULLONG_MAX);
printf("\n== Tipe exact-width (stdint.h) ==\n");
printf("INT8_MAX = %d\n", INT8_MAX);
printf("INT16_MAX = %d\n", INT16_MAX);
printf("INT32_MAX = %d\n", INT32_MAX);
printf("INT64_MAX = %" PRId64 "\n", INT64_MAX);
printf("UINT8_MAX = %u\n", UINT8_MAX);
printf("UINT32_MAX = %u\n", UINT32_MAX);
printf("UINT64_MAX = %" PRIu64 "\n", UINT64_MAX);
printf("\n== Floating point (float.h) ==\n");
printf("FLT_MIN = %e\n", FLT_MIN);
printf("FLT_MAX = %e\n", FLT_MAX);
printf("FLT_EPSILON = %e\n", FLT_EPSILON);
printf("FLT_DIG = %d\n", FLT_DIG);
printf("DBL_MIN = %e\n", DBL_MIN);
printf("DBL_MAX = %e\n", DBL_MAX);
printf("DBL_EPSILON = %e\n", DBL_EPSILON);
printf("DBL_DIG = %d\n", DBL_DIG);
printf("LDBL_DIG = %d\n", LDBL_DIG);
printf("\n== Pointer-sized integers ==\n");
printf("PTRDIFF_MIN = %td\n", PTRDIFF_MIN);
printf("PTRDIFF_MAX = %td\n", PTRDIFF_MAX);
printf("SIZE_MAX = %zu\n", SIZE_MAX);
return 0;
}Compile dan jalankan:
gcc -std=c11 -Wall -Wextra -o tipe1 tipe1.c
./tipe1Output yang Dihasilkan:
== Ukuran tipe dasar ==
sizeof(char) = 1 byte
sizeof(short) = 2 byte
sizeof(int) = 4 byte
sizeof(long) = 8 byte
sizeof(long long) = 8 byte
sizeof(float) = 4 byte
sizeof(double) = 8 byte
sizeof(long double) = 16 byte
sizeof(void*) = 8 byte
sizeof(size_t) = 8 byte
== Rentang signed integer ==
INT_MIN = -2147483648
INT_MAX = 2147483647
LONG_MIN = -9223372036854775808
LONG_MAX = 9223372036854775807
LLONG_MAX = 9223372036854775807
== Rentang unsigned integer ==
UINT_MAX = 4294967295
ULONG_MAX = 18446744073709551615
ULLONG_MAX = 18446744073709551615
== Tipe exact-width (stdint.h) ==
INT8_MAX = 127
INT16_MAX = 32767
INT32_MAX = 2147483647
INT64_MAX = 9223372036854775807
UINT8_MAX = 255
UINT32_MAX = 4294967295
UINT64_MAX = 18446744073709551615
== Floating point (float.h) ==
FLT_MIN = 1.175494e-38
FLT_MAX = 3.402823e+38
FLT_EPSILON = 1.192093e-07
FLT_DIG = 6
DBL_MIN = 2.225074e-308
DBL_MAX = 1.797693e+308
DBL_EPSILON = 2.220446e-16
DBL_DIG = 15
LDBL_DIG = 18
== Pointer-sized integers ==
PTRDIFF_MIN = -9223372036854775808
PTRDIFF_MAX = 9223372036854775807
SIZE_MAX = 18446744073709551615Mari kita telaah baris-baris kode di atas:
- Baris 7 (
#include <inttypes.h>): Header ini menyediakan macroPRId64,PRIu64, dan lainnya yang merupakan string format specifier portabel untuk tipestdint.h. Tanpa macro ini, kita harus memilih%ldatau%lldsecara manual—dan pilihan bisa salah antar platform. - Baris 12-21 (
sizeof):sizeofadalah operator compile-time yang mengembalikan ukuran dalam satuanchar(biasanya byte). Tipe kembaliannyasize_t—kita pakai%zusebagai specifier. - Baris 33-35 (
INT64_MAX): Pada LP64 Linux,int64_tadalah alias untuklong, sehingga%lldakan memicu warning. MacroPRId64otomatis memilih%ldatau%lldsesuai platform—praktik portabel yang harus kita ikuti. - Baris 47 (
FLT_EPSILON): Nilai ini adalah selisih terkecil antara1.0fdan bilanganfloatberikutnya yang dapat dibedakan. Setiap perbandingan float yang lebih kecil dariFLT_EPSILONdianggap nol. - Baris 55-57 (
PTRDIFF_MIN,PTRDIFF_MAX,SIZE_MAX): Tipeptrdiff_tdansize_tdijamin cukup besar untuk menyimpan selisih dan ukuran pointer. Pada LP64, keduanya 8 byte; pada platform 32-bit, keduanya 4 byte.