首页 > 其他分享 >C语言整数类型的存储空间和取值范围

C语言整数类型的存储空间和取值范围

时间:2024-09-22 19:24:30浏览次数:7  
标签:int MAX Self long C语言 def printf 取值 存储空间

C语言整数类型的存储空间和取值范围

  1. 四种整数类型 char, short, int, long 默认有符号,再加上无符号限制,共8种情况
              char // 字符型,单字节,取值范围:CHAR_MIN - CHAR_MAX
     unsigned char // 无符号字符型,  取值范围:0 - UCHAR_MAX
         short int // 短整型,双字节,取值范围:SHRT_MIN - SHRT_MAX
unsigned short int // 无符号短整型,  取值范围:0 - USHRT_MAX
               int // 整型,四字节,  取值范围:INT_MIN - INT_MAX
      unsigned int // 无符号整型,    取值范围:0 - UINT_MAX
          long int // 长整型,八字节,取值范围:LONG_MIN - LONG_MAX
 unsigned long int // 无符号长整型,  取值范围: 0 - ULONG_MAX

在Linux下测试发现long long,即长长整型与long长整型所表达的效果是一样的!!!

  1. 写代码输出整数类型的存储空间和取值范围相关信息
    注意:相关的宏定义在limits.h头文件中,要包含进来!
/* filename: limits.c */
#include <stdio.h>
#include <limits.h>

/*
  compile : gcc limits.c -o limits
      run : ./limits
*/

/* output datatype char, short, int, long
   sizeof, min, max, unsigned max information */
void
out_smmu_info (void)
{
  printf ("Sizeof char is %lu\n", sizeof (char));
  printf ("Min char is %d\n", CHAR_MIN);
  printf ("Max char is %d\n", CHAR_MAX);
  printf ("Max unsigned char is %u\n", UCHAR_MAX);
  printf ("--------------------------------\n");
  printf ("Sizeof short is %lu\n", sizeof (short));
  printf ("Min short is %d\n", SHRT_MIN);
  printf ("Max short is %d\n", SHRT_MAX);
  printf ("Max unsigned short is %u\n", USHRT_MAX);
  printf ("--------------------------------\n");
  printf ("Sizeof int is %lu\n", sizeof (int));
  printf ("Min int is %d\n", INT_MIN);
  printf ("Max int is %d\n", INT_MAX);
  printf ("Max unsigned int is %u\n", UINT_MAX);
  printf ("--------------------------------\n");
  printf ("Sizeof long is %lu\n", sizeof (long));
  printf ("Min long is %ld\n", LONG_MIN);
  printf ("Max long is %ld\n", LONG_MAX);
  printf ("Max unsigned long is %lu\n", ULONG_MAX);
  printf ("--------------------------------\n");
}

/* output char, short, int, long
   min, max, unsigned max value, define myself use hex */
void
out_selfdef_info (void)
{
  printf ("Self def: CHAR_MIN ==> 0X80, %d\n", (char)(0X80));
  printf ("Self def: CHAR_MAX ==> 0X7F, %d\n", (char)(0X7F));
  printf ("Self def: UCHAR_MAX ==> 0XFF, %u\n", (unsigned char)(0XFF));
  printf ("-------------------------------------------------------\n");
  printf ("Self def: SHORT_MIN ==> 0X8000, %d\n", (short)(0X8000));
  printf ("Self def: SHORT_MAX ==> 0X7FFF, %d\n", (short)(0X7FFF));
  printf ("Self def: USHORT_MAX ==> 0XFFFF, %u\n", (unsigned short)(0XFFFF));
  printf ("-------------------------------------------------------\n");
  printf ("Self def: INT_MIN ==> 0X80000000, %d\n", (int)(0X80000000));
  printf ("Self def: INT_MAX ==> 0X7FFFFFFF, %d\n", (int)(0X7FFFFFFF));
  printf ("Self def: UINT_MAX ==> 0XFFFF, %u\n", (unsigned int)(0XFFFFFFFF));
  printf ("-------------------------------------------------------\n");
  printf ("Self def: LONG_MIN ==> 0X8000000000000000, %ld\n",
	  (long)(0X8000000000000000));
  printf ("Self def: LONG_MAX ==> 0X7FFFFFFFFFFFFFFF, %ld\n",
	  (long)(0X7FFFFFFFFFFFFFFF));
  printf ("Self def: ULONG_MAX ==> 0XFFFFFFFFFFFFFFFF, %lu\n",
	  (unsigned long)(0XFFFFFFFFFFFFFFFF));
  printf ("-------------------------------------------------------\n");
}

/**/
int
main (int argc, char *argv[])
{
  out_smmu_info ();
  out_selfdef_info ();
  return 0;
}
/* --(:-:)-- */
  1. 编译运行,输出结果如下:
gwsong@ubuntu:~/works/zoo/tmp$ gcc limits.c -o limits
gwsong@ubuntu:~/works/zoo/tmp$ ./limits
Sizeof char is 1
Min char is -128
Max char is 127
Max unsigned char is 255
--------------------------------
Sizeof short is 2
Min short is -32768
Max short is 32767
Max unsigned short is 65535
--------------------------------
Sizeof int is 4
Min int is -2147483648
Max int is 2147483647
Max unsigned int is 4294967295
--------------------------------
Sizeof long is 8
Min long is -9223372036854775808
Max long is 9223372036854775807
Max unsigned long is 18446744073709551615
--------------------------------
Self def: CHAR_MIN ==> 0X80, -128
Self def: CHAR_MAX ==> 0X7F, 127
Self def: UCHAR_MAX ==> 0XFF, 255
-------------------------------------------------------
Self def: SHORT_MIN ==> 0X8000, -32768
Self def: SHORT_MAX ==> 0X7FFF, 32767
Self def: USHORT_MAX ==> 0XFFFF, 65535
-------------------------------------------------------
Self def: INT_MIN ==> 0X80000000, -2147483648
Self def: INT_MAX ==> 0X7FFFFFFF, 2147483647
Self def: UINT_MAX ==> 0XFFFF, 4294967295
-------------------------------------------------------
Self def: LONG_MIN ==> 0X8000000000000000, -9223372036854775808
Self def: LONG_MAX ==> 0X7FFFFFFFFFFFFFFF, 9223372036854775807
Self def: ULONG_MAX ==> 0XFFFFFFFFFFFFFFFF, 18446744073709551615
-------------------------------------------------------
  1. 用十六进制自定义取值范围清晰明了,不同情况下可以选择不同长度的整数数据类型
    下一步研究一下其他数据类型的存储长度和相关信息。

标签:int,MAX,Self,long,C语言,def,printf,取值,存储空间
From: https://www.cnblogs.com/gwsong52/p/18425724

相关文章

  • C语言进阶【5】---数据在内存中的存储【2】(小数存储很难吗?)
    本章概述本章引要练习浮点数的存储浮点数的取出小补充题目解析彩蛋时刻!!!本章引要常见的浮点数:3.1415,1E10等。其中,1E10是科学计数法的形式,它也就等于1*10^10。小数数据类型:float,double,longdouble。练习在开讲本章内容前,大家先来看个代码,大家先猜一下结果:#de......
  • 20240909_181725 c语言 标识符
    什么是标识符命名规定标识符特性区分大小写命名约定演练小结......
  • C语言深入理解指针(四)
    目录字符指针变量数组指针变量数组指针变量是什么数组指针变量怎么初始化二维数组传参的本质函数指针变量函数指针变量的创建函数指针变量的使用代码typedef关键字函数指针数组转移表字符指针变量字符指针在之前我们有提到过,(字符)(指针)前面的字符代表着存储的元素为......
  • c语言-数据类型
    1.C语言的简介  是一门面向过程的编程语言   1972年贝尔实验室 丹尼斯里奇   ...//百度百科https://baike.baidu.com/item/c%E8%AF%AD%E8%A8%80/105958?fr=ge_ala2.问题引入   1)我们所有要处理的这些数据文件(视频,音乐,图片等)    ......
  • C语言中if else组合
    一bool变量与“零值”进行比较bool变量与“零值”进行比较的if语句怎么写?boolbTestFlag=FALSE;//想想为什么一般初始化为FALSE比较好?A),if(bTestFlag==0);if(bTestFlag==1);B),if(bTestFlag==TRUE);if(bTestFlag==FLASE);C),if(bTestFlag);if(!bT......
  • C语言内容函数大揭秘:轻松掌握,编程无忧(下)
    大家们好,废话不多说,我们接着继续来讲我们函数的章节。六.数组做函数参数在使用函数解决问题的时候,难免会将数组作为参数传递给函数,在函数内部对数组进行操作。比如:写一个函数将一个整型数组的内容,全部置为﹣1,再写一个函数打印数组的内容。1#include<stdio.h>23int......
  • 了解C语言
    1.什么是C语言C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目的是提供一种能以简易的方式编程,处理低级储存器,产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。C语言是一门面向过程的计算机编程语言,C++,JAVA等面向对象的编程语言有所不同。......
  • C语言函数的形参传递
    在C语言中,函数形参的传递分为值传递和地址传递(指针传递)。这两种传递方式决定了函数内如何使用这些参数,并且在函数外部的影响也不同。下面详细解释C语言中形参的传递机制和具体内容。1.值传递在C语言中,默认的传递方式是值传递。当我们调用一个函数时,函数的形参接收......
  • C语言的文件函数
    此篇文章主要对C语言中的"文件读写函数"进行详细的刨析~通过此篇文章能够了解并学习到:"字符读写函数","文本行读写函数 ","格式化读写函数","二进制读写函数","文件随机读取函数 ","读取结束原因函数"等相关知识~(上一篇:文件基础知识传送门:C语言的文件基础知识-CSDN......
  • C语言-文件操作-一些我想到的、见到的奇怪的问题
    博客主页:【夜泉_ly】本文专栏:【C语言】欢迎点赞......