C 数据类型
了解C - C数据类型
下表总结了变量类型。
类型 | 典型字节数 | 典型的值范围 |
---|---|---|
char | 1 | -128至+127 或 0至+255 |
unsigned char | 1 | 0至+255 |
short | 2 | -32,768至+32,767 |
unsigned short | 2 | 0至+65,535 |
int | 2 或 4 | -32,768至+32,767 或 -2,147,438,648至+2,147,438,647 |
unsigned int | 4 | 0至+65,535 或 0至+4,294,967,295 |
long | 4 | -2,147,438,648至+2,147,438,647 |
unsigned long | 4 | 0至+4,294,967,295 |
long long | 8 | -9,223,372,036,854,775,808至+9,223,372,036,854,775,807 |
unsigned long long | 8 | 0至+18,446,744,073,709,551,615 |
float | 4 | +/- 3.4E +/- 38(6位数) |
double | 8 | +/- 1.7E +/- 308(15位数) |
long double | 12 | +/- 1.2E +/- 4932(19位数) |
局限性
一些符号常量来自limit.h
符号常量 | 表示 |
---|---|
CHAR_BIT | char中的位数 |
CHAR_MAX | 最大char值 |
CHAR_MIN | 最小char值 |
SCHAR_MAX | 最大带符号char值 |
SCHAR_MIN | 最小带符号char值 |
UCHAR_MAX | 最大无符号char值 |
SHRT_MAX | 最大short值 |
SHRT_MIN | 最小short值 |
USHRT_MAX | 最大无符号short值 |
INT_MAX | 最大int值 |
INT_MIN | 最小int值 |
UINT_MAX | 最大无符号int值 |
LONG_MAX | 最大long值 |
LONG_MIN | 最小long值 |
ULONG_MAX | 最大无符号long值 |
LLONG_MAX | 最大long long值 |
LLONG_MIN | 最小long long值 |
ULLONG_MAX | 最大无符号long long值 |
使用从limit.h和float定义的常量。
#include <stdio.h>
#include <limits.h> // integer limits
#include <float.h> // floating-point limits
int main(void)
{
printf("Some number limits for this system:\n");
printf("Biggest int: %d\n", INT_MAX);
printf("Smallest long long: %lld\n", LLONG_MIN);
printf("One byte = %d bits on this system.\n", CHAR_BIT);
printf("Largest double: %e\n", DBL_MAX);
printf("Smallest normal float: %e\n", FLT_MIN);
printf("float precision = %d digits\n", FLT_DIG);
printf("float epsilon = %e\n", FLT_EPSILON);
return 0;
}
上面的代码生成以下结果。
例子
将2英寻转换成英尺
#include <stdio.h>
int main(void)
{
int feet, fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n", 6 * fathoms);
return 0;
}
上面的代码生成以下结果。
例2
以下代码显示了如何从命令行读取int值。
#include <stdio.h>
int main(void)
{
int dogs;
printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);
return 0;
}
上面的代码生成以下结果。
例3
以下代码显示如何使用整数类型的可移植名称。
#include <stdio.h>
#include <inttypes.h> // supports portable types
int main(void)
{
int32_t me32; // me32 a 32-bit signed variable
me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d\n", me32);
printf("Next, let"s not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n", me32);
return 0;
}
上面的代码生成以下结果。
例4
下面的代码显示了如何以十进制,八进制和十六进制打印100。
#include <stdio.h>
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);
return 0;
}
上面的代码生成以下结果。
例5
下面的代码声明和使用各种int类型。
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* system with 32-bit int */
short end = 200; /* and 16-bit short */
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig= %lld and not %ld\n", verybig, verybig);
return 0;
}
上面的代码生成以下结果。
例6
下面的代码以两种方式显示浮点值。
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
// next line requires C99 or later compliance
printf("And it"s %a in hexadecimal, powers of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);
return 0;
}
上面的代码生成以下结果。
例7
以下代码显示如果值超过最大int大小会发生什么。
#include <stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;
printf("%d %d %d\n", i, i+1, i+2);
printf("%u %u %u\n", j, j+1, j+2);
return 0;
}
上面的代码生成以下结果。
例8
以下代码打印出类型大小。
#include <stdio.h>
int main(void)
{
/* c99 provides a %zd specifier for sizes */
printf("Type int has a size of %zd bytes.\n", sizeof(int));
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type long has a size of %zd bytes.\n", sizeof(long));
printf("Type long long has a size of %zd bytes.\n",
sizeof(long long));
printf("Type double has a size of %zd bytes.\n",
sizeof(double));
printf("Type long double has a size of %zd bytes.\n",
sizeof(long double));
return 0;
}
上面的代码生成以下结果。
例9
#include <stdio.h>
#include <stdint.h>
#include <wchar.h>
#include <limits.h>
#include <float.h>
#include <math.h>
int main(void)
{
printf("PTRDIFF_MIN = %td\n", PTRDIFF_MIN);
printf("PTRDIFF_MAX = %+td\n", PTRDIFF_MAX);
printf("SIZE_MAX = %zu\n", SIZE_MAX);
printf("SIG_ATOMIC_MIN = %+jd\n",(intmax_t)SIG_ATOMIC_MIN);
printf("SIG_ATOMIC_MAX = %+jd\n",(intmax_t)SIG_ATOMIC_MAX);
printf("WCHAR_MIN = %+jd\n",(intmax_t)WCHAR_MIN);
printf("WCHAR_MAX = %+jd\n",(intmax_t)WCHAR_MAX);
printf("WINT_MIN = %jd\n", (intmax_t)WINT_MIN);
printf("WINT_MAX = %jd\n", (intmax_t)WINT_MAX);
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("MB_LEN_MAX = %d\n", MB_LEN_MAX);
printf("\n");
printf("CHAR_MIN = %+d\n", CHAR_MIN);
printf("CHAR_MAX = %+d\n", CHAR_MAX);
printf("SCHAR_MIN = %+d\n", SCHAR_MIN);
printf("SCHAR_MAX = %+d\n", SCHAR_MAX);
printf("UCHAR_MAX = %u\n", UCHAR_MAX);
printf("\n");
printf("SHRT_MIN = %+d\n", SHRT_MIN);
printf("SHRT_MAX = %+d\n", SHRT_MAX);
printf("USHRT_MAX = %u\n", USHRT_MAX);
printf("\n");
printf("INT_MIN = %+d\n", INT_MIN);
printf("INT_MAX = %+d\n", INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
printf("\n");
printf("LONG_MIN = %+ld\n", LONG_MIN);
printf("LONG_MAX = %+ld\n", LONG_MAX);
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("\n");
printf("LLONG_MIN = %+lld\n", LLONG_MIN);
printf("LLONG_MAX = %+lld\n", LLONG_MAX);
printf("ULLONG_MAX = %llu\n", ULLONG_MAX);
printf("\n");
printf("FLT_RADIX = %d\n", FLT_RADIX);
printf("DECIMAL_DIG = %d\n", DECIMAL_DIG);
printf("FLT_MIN = %e\n", FLT_MIN);
printf("FLT_MAX = %e\n", FLT_MAX);
printf("FLT_EPSILON = %e\n", FLT_EPSILON);
printf("FLT_DIG = %d\n", FLT_DIG);
printf("FLT_MANT_DIG = %d\n", FLT_MANT_DIG);
printf("FLT_MIN_EXP = %d\n", FLT_MIN_EXP);
printf("FLT_MIN_10_EXP = %d\n", FLT_MIN_10_EXP);
printf("FLT_MAX_EXP = %d\n", FLT_MAX_EXP);
printf("FLT_MAX_10_EXP = %d\n", FLT_MAX_10_EXP);
printf("FLT_ROUNDS = %d\n", FLT_ROUNDS);
printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
}
上面的代码生成以下结果。