One - One Code All

Blog Content

C 语言实例 - 计算 int, float, double 和 char 字节大小

每日一练 C/C++   2007-02-19 19:26:05
/*
* 使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。
sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++、--等,它并不是函数。
sizeof 操作符以字节形式给出了其操作数的存储大小。
*/
#include
int main()
{
   int integerType;
   float floatType;
   double doubleType;
   char charType;

   int a;
   long b;
   long long c;
   double e;
   long double f;
   // sizeof 操作符用于计算变量的字节大小
   printf("Size of int: %ld bytes\n",sizeof(integerType));
   printf("Size of float: %ld bytes\n",sizeof(floatType));
   printf("Size of double: %ld bytes\n",sizeof(doubleType));
   printf("Size of char: %ld byte\n",sizeof(charType));

   printf("Size of int = %ld bytes \n", sizeof(a));
   printf("Size of long = %ld bytes\n", sizeof(b));
   printf("Size of long long = %ld bytes\n", sizeof(c));
   printf("Size of double = %ld bytes\n", sizeof(e));
   printf("Size of long double = %ld bytes\n", sizeof(f));

   printf("\n");
   return 0;
}


输出:

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Size of int = 4 bytes
Size of long = 8 bytes
Size of long long = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes


上一篇:C 语言实例 - 数值比较
下一篇:C 语言实例 - 交换两个数的值

The minute you think of giving up, think of the reason why you held on so long.