首页 > 代码库 > C程序设计语言部分习题

C程序设计语言部分习题

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