下表总结了变量类型。
类型 | 典型字节数 | 典型的值范围 |
---|---|---|
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: %dn", INT_MAX);
printf("Smallest long long: %lldn", LLONG_MIN);
printf("One byte = %d bits on this system.n", CHAR_BIT);
printf("Largest double: %en", DBL_MAX);
printf("Smallest normal float: %en", FLT_MIN);
printf("float precision = %d digitsn", FLT_DIG);
printf("float epsilon = %en", 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;
}
上面的代码生成以下结果。
以下代码显示了如何从命令行读取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;
}
上面的代码生成以下结果。
以下代码显示如何使用整数类型的可移植名称。
#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 = %dn", 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;
}
上面的代码生成以下结果。
下面的代码显示了如何以十进制,八进制和十六进制打印100。
#include <stdio.h>
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %xn", x, x, x);
printf("dec = %d; octal = %#o; hex = %#xn", x, x, x);
return 0;
}
上面的代码生成以下结果。
下面的代码声明和使用各种int类型。
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000;
short end = 200;
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %dn", un, un);
printf("end = %hd and %dn", end, end);
printf("big = %ld and not %hdn", big, big);
printf("verybig= %lld and not %ldn", verybig, verybig);
return 0;
}
上面的代码生成以下结果。
下面的代码以两种方式显示浮点值。
#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 %en", aboat, aboat);
// next line requires C99 or later compliance
printf("And it"s %a in hexadecimal, powers of 2 notationn", aboat);
printf("%f can be written %en", abet, abet);
printf("%Lf can be written %Len", dip, dip);
return 0;
}
上面的代码生成以下结果。
以下代码显示如果值超过最大int大小会发生什么。
#include <stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;
printf("%d %d %dn", i, i+1, i+2);
printf("%u %u %un", j, j+1, j+2);
return 0;
}
上面的代码生成以下结果。
以下代码打印出类型大小。
#include <stdio.h>
int main(void)
{
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;
}
上面的代码生成以下结果。
#include <stdio.h>
#include <stdint.h>
#include <wchar.h>
#include <limits.h>
#include <float.h>
#include <math.h>
int main(void)
{
printf("PTRDIFF_MIN = %tdn", PTRDIFF_MIN);
printf("PTRDIFF_MAX = %+tdn", PTRDIFF_MAX);
printf("SIZE_MAX = %zun", SIZE_MAX);
printf("SIG_ATOMIC_MIN = %+jdn",(intmax_t)SIG_ATOMIC_MIN);
printf("SIG_ATOMIC_MAX = %+jdn",(intmax_t)SIG_ATOMIC_MAX);
printf("WCHAR_MIN = %+jdn",(intmax_t)WCHAR_MIN);
printf("WCHAR_MAX = %+jdn",(intmax_t)WCHAR_MAX);
printf("WINT_MIN = %jdn", (intmax_t)WINT_MIN);
printf("WINT_MAX = %jdn", (intmax_t)WINT_MAX);
printf("CHAR_BIT = %dn", CHAR_BIT);
printf("MB_LEN_MAX = %dn", MB_LEN_MAX);
printf("n");
printf("CHAR_MIN = %+dn", CHAR_MIN);
printf("CHAR_MAX = %+dn", CHAR_MAX);
printf("SCHAR_MIN = %+dn", SCHAR_MIN);
printf("SCHAR_MAX = %+dn", SCHAR_MAX);
printf("UCHAR_MAX = %un", UCHAR_MAX);
printf("n");
printf("SHRT_MIN = %+dn", SHRT_MIN);
printf("SHRT_MAX = %+dn", SHRT_MAX);
printf("USHRT_MAX = %un", USHRT_MAX);
printf("n");
printf("INT_MIN = %+dn", INT_MIN);
printf("INT_MAX = %+dn", INT_MAX);
printf("UINT_MAX = %un", UINT_MAX);
printf("n");
printf("LONG_MIN = %+ldn", LONG_MIN);
printf("LONG_MAX = %+ldn", LONG_MAX);
printf("ULONG_MAX = %lun", ULONG_MAX);
printf("n");
printf("LLONG_MIN = %+lldn", LLONG_MIN);
printf("LLONG_MAX = %+lldn", LLONG_MAX);
printf("ULLONG_MAX = %llun", ULLONG_MAX);
printf("n");
printf("FLT_RADIX = %dn", FLT_RADIX);
printf("DECIMAL_DIG = %dn", DECIMAL_DIG);
printf("FLT_MIN = %en", FLT_MIN);
printf("FLT_MAX = %en", FLT_MAX);
printf("FLT_EPSILON = %en", FLT_EPSILON);
printf("FLT_DIG = %dn", FLT_DIG);
printf("FLT_MANT_DIG = %dn", FLT_MANT_DIG);
printf("FLT_MIN_EXP = %dn", FLT_MIN_EXP);
printf("FLT_MIN_10_EXP = %dn", FLT_MIN_10_EXP);
printf("FLT_MAX_EXP = %dn", FLT_MAX_EXP);
printf("FLT_MAX_10_EXP = %dn", FLT_MAX_10_EXP);
printf("FLT_ROUNDS = %dn", FLT_ROUNDS);
printf("FLT_EVAL_METHOD = %dn", FLT_EVAL_METHOD);
}
上面的代码生成以下结果。
C if 语句 C 判断一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。语法C 语言中 if 语句的语法:if(boolean_expression){...
C 从函数返回数组 C 数组C 语言不允许返回一个完整的数组作为函数的参数。但是,您可以通过指定不带索引的数组名来返回一个指向...
C 练习实例25 C 语言经典100例题目:求1+2!+3!+...+20!的和。程序分析:此程序只是把累加变成了累乘。 程序源代码://Created by...
C 练习实例39 C 语言经典100例题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。程序分析:首先判...