Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ชนิดข้อมูล size_t ใน C

size_t เป็น unsigned integral type ใช้แทนขนาดของวัตถุในหน่วย bytes คืนค่าจาก sizeof operator ใช้สำหรับ array indexing และ counting ไม่มีค่าลบ

Return type ของ strcspn, strlen เป็น size_t

ไวยากรณ์

const size_t var_name;

ตัวอย่าง

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
    const size_t x = 150;
    int a[x];
    for(size_t i = 0; i < x; ++i)
        a[i] = i;
    printf("SIZE_MAX = %lu\n", SIZE_MAX);
    size_t size = sizeof(a);
    printf("size = %zu\n", size);
    return 0;
}

ผลลัพธ์

SIZE_MAX = 18446744073709551615
size = 600

size_t ใช้ประกาศขนาดอาร์เรย์ (VLA) และเก็บผลลัพธ์ sizeof (600 bytes สำหรับ int[150])