C语言中输出字节数的方法是使用sizeof
运算符。sizeof
运算符返回指定类型或变量的大小,单位为字节。
以下是使用sizeof
运算符输出字节数的示例代码:
#includeint main() { int num; float f; char c; double d; printf("Size of int: %zu bytes\n", sizeof(num)); printf("Size of float: %zu bytes\n", sizeof(f)); printf("Size of char: %zu bytes\n", sizeof(c)); printf("Size of double: %zu bytes\n", sizeof(d)); return 0; }
输出结果:
Size of int: 4 bytes Size of float: 4 bytes Size of char: 1 byte Size of double: 8 bytes
注意:sizeof
运算符的结果类型是size_t
,使用%zu
格式说明符进行打印。