sizeof เป็นตัวดำเนินการยูนารี (unary operator) เวลาคอมไพล์ ใช้คำนวณขนาดของ operand ในหน่วยไบต์ ใช้กับชนิดข้อมูล, ตัวแปร, วิธีการ, พอยน์เตอร์ได้ ผลลัพธ์อาจต่างกันระหว่างระบบ 32-bit และ 64-bit
ตัวอย่างกับชนิดข้อมูล
#include <stdio.h>
int main() {
int a = 16;
printf("Size of variable a : %zu\n", sizeof(a));
printf("Size of int data type : %zu\n", sizeof(int));
printf("Size of char data type : %zu\n", sizeof(char));
printf("Size of float data type : %zu\n", sizeof(float));
printf("Size of double data type : %zu\n", sizeof(double));
return 0;
}ผลลัพธ์ (ตัวอย่าง 64-bit)
Size of variable a : 4 Size of int data type : 4 Size of char data type : 1 Size of float data type : 4 Size of double data type : 8
ตัวอย่างกับนิพจน์
#include <stdio.h>
int main() {
char a = 'S';
double b = 4.65;
printf("Size of variable a : %zu\n", sizeof(a));
printf("Size of an expression : %zu\n", sizeof(a + b));
int s = (int)(a + b);
printf("Size of explicitly converted expression : %zu\n", sizeof(s));
return 0;
}ผลลัพธ์
Size of variable a : 1 Size of an expression : 8 Size of explicitly converted expression : 4