吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的数据类型

C中的数据类型

来源:网络 | 更新时间:2022-01-08 06:58:18
C 中的每个变量都有一个关联的数据类型。每种数据类型需要不同数量的内存,并且有一些可以在其上执行的特定操作。让我们一一简要描述: 以下是 C 中使用的一些非常常见的数据类型的示例:
  • char: C 中最基本的数据类型。它存储单个字符,在几乎所有编译器中都需要一个字节的内存。
  • int:顾名思义,一个int变量用于存储一个整数。
  • float:用于存储单精度十进制数(具有浮点值的数字)。
  • double:用于以双精度存储十进制数(具有浮点值的数字)。 
不同的数据类型也有不同的范围,它们可以存储数字。这些范围可能因编译器而异。下面是范围列表以及 32 位 gcc 编译器上的内存要求和格式说明符。
Data Type Memory (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 %f
double 8 %lf
long double 16 %Lf
  我们可以使用sizeof() 运算符来检查变量的大小。有关各种数据类型的用法,请参见以下 C 程序:
#include <stdio.h>
int main()
{
int a = 1;
char b = 'G';
double c = 3.14;
printf("Hello World!n");

// printing the variables defined
// above along with their sizes
printf("Hello! I am a character. My value is %c and "
"my size is %lu byte.n",
b, sizeof(char));
// can use sizeof(b) above as well

printf("Hello! I am an integer. My value is %d and "
"my size is %lu bytes.n",
a, sizeof(int));
// can use sizeof(a) above as well

printf("Hello! I am a double floating point variable."
" My value is %lf and my size is %lu bytes.n",
c, sizeof(double));
// can use sizeof(c) above as well

printf("Bye! See you soon. :)n");

return 0;
}
输出: 
Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4  bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my size i
s 8 bytes.
Bye! See you soon. :)
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com