码迷,mamicode.com
首页 > 其他好文 > 详细

C程序设计语言部分习题

时间:2014-08-06 14:14:51      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   文件   ar   div   log   

1:编写一个程序,确定分别由signed ,unsigned 限定的char short long int的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现。

头文件的实现方式:

#include <stdio.h>
#include <limits.h>


/*determin range of int types*/
int main(){
  /*signed*/
  printf("signed char min = %d\n",SCHAR_MIN);
  printf("signed char max = %d\n",SCHAR_MAX);
  
  printf("signed short min = %d\n",SHRT_MIN);
  printf("signed short max = %d\n",SHRT_MAX);
  
  printf("signed int min = %d\n",INT_MIN);
  printf("signed int max = %d\n",INT_MAX);
  
  printf("signed long min = %ld\n",LONG_MIN);
  printf("signed long max = %ld\n",LONG_MAX);
  
  /*unsigned*/
  printf("signed char max=%u\n",UCHAR_MAX);

  printf("signed short max = %u\n",USHRT_MAX);
  
  printf("signed int max = %u\n",UINT_MAX);
  
  printf("signed long max = %lu\n",ULONG_MAX);
  
  getchar();
    
  return 0;
}

 直接计算:

#include <stdio.h>
/*
 ~0 取反 全部变成1
(unsigned char)~0 变成无符号的
(unsigned char)~0>>1 右移一位清除符号位
(char)(unsigned char)~0>>1 转化为char类型
 */


int main(){
  
  /*singed*/
  
  printf("min of singed char = %d\n",-(char)((unsigned char)~0>>1));
  printf("max of singed char = %d\n",(char)((unsigned char)~0>>1));
  
  printf("min of singed short = %d\n",-(short)((unsigned short)~0>>1));
  printf("max of singed short = %d\n",(short)((unsigned short)~0>>1));
  
  printf("min of singed int = %ld\n",-(int)((int long)~0>>1));
  printf("max of singed int = %ld\n",(int)((int long)~0>>1));
  
  printf("min of singed long = %ld\n",-(long)((unsigned long)~0>>1));
  printf("max of singed long = %ld\n",(long)((unsigned long)~0>>1));
  
  
  /*unsigned*/
  printf("max of singed char = %u\n",(unsigned char)~0);
  printf("max of singed char = %u\n",(unsigned short)~0);
  printf("max of singed char = %u\n",(unsigned int)~0);
  printf("max of singed char = %lu\n",(unsigned long)~0);
  
  getchar();
  
  return 0 ; 
}

 


 

C程序设计语言部分习题,布布扣,bubuko.com

C程序设计语言部分习题

标签:style   blog   color   io   文件   ar   div   log   

原文地址:http://www.cnblogs.com/luochuanghero/p/3894165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!