C 数据类型

了解C - C数据类型

下表总结了变量类型。

类型典型字节数典型的值范围
char1-128至+127 或 0至+255
unsigned char10至+255
short2-32,768至+32,767
unsigned short20至+65,535
int2 或 4-32,768至+32,767 或 -2,147,438,648至+2,147,438,647
unsigned int40至+65,535 或 0至+4,294,967,295
long4-2,147,438,648至+2,147,438,647
unsigned long40至+4,294,967,295
long long8-9,223,372,036,854,775,808至+9,223,372,036,854,775,807
unsigned long long80至+18,446,744,073,709,551,615
float4+/- 3.4E +/- 38(6位数)
double8+/- 1.7E +/- 308(15位数)
long double12+/- 1.2E +/- 4932(19位数)

局限性

一些符号常量来自limit.h

符号常量表示
CHAR_BITchar中的位数
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);
}

上面的代码生成以下结果。