举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > c数据类型有哪些 C 数据类型

c数据类型有哪些 C 数据类型

2023-05-29 11:18 C语言教程

c数据类型有哪些 C 数据类型

c数据类型有哪些 C 数据类型

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: %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;
}

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

例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 = %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;
}

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

例4

下面的代码显示了如何以十进制,八进制和十六进制打印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;
}

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

例5

下面的代码声明和使用各种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;
}

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

例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 %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;
}

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

例7

以下代码显示如果值超过最大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;
}

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

例8

以下代码打印出类型大小。


#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;
}

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

例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    = %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数据类型有哪些 C 数据类型全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部