size_t
– kiểu kích thước – là kiểu số nguyên không âm có độ rộng tùy thuộc vào hỗ trợ của hệ điều hành và không nhỏ hơn 16-bit
. size_t
được định nghĩa trong các thư viện:
<stddef.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
<uchar.h>
<wchar.h>
Trong code C
tiêu chuẩn, kiểu size_t
thường được sử dụng cho các biến trị số index
để truy xuất mảng và các biến counter
đếm vòng lặp thay cho kiểu số nguyên không dấu unsigned int
. Lý do là vì thiết kế của unsigned int
có độ rộng cố định là 32-bit
và có thể sẽ bị tràn giá trị nếu sử dụng để đếm các bộ dữ liệu hay các vòng lặp lớn trong các chương trình 64-bit
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdio.h></span></span> <span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdint.h></span></span> <span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><limits.h></span></span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> <span class="token keyword">int</span> $argc<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> $argv<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">size_t</span> $size <span class="token operator">=</span> SIZE_MAX<span class="token punctuation">;</span> <span class="token comment">// stdint.h</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> $uint <span class="token operator">=</span> UINT_MAX<span class="token punctuation">;</span> <span class="token comment">// limits.h</span> <span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">"Max value of size_t : %zu n"</span><span class="token punctuation">,</span> $size<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">"Max value of unsigned int : %u n"</span><span class="token punctuation">,</span> $uint<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
1 2 3 4 | # ubuntu 64-bit Max value of size_t : 18446744073709551615 Max value of unsigned int : 4294967295 |
Ngoài ra
size_t
còn được sử dụng tại các vị trí định kiểu giá trị trả về và các tham số của các hàm trong thư viện tiêu chuẩn ví dụ như sizeof
, offsetof
, bsearch
, qsort
, v.v…1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void* bsearch ( const void* key, const void* ptr, size_t count, size_t size, int (*comp) (const void*, const void*) ); void qsort ( void* ptr, size_t count, size_t size, int (*comp) (const void*, const void*) ); |