C程式設計語言部分習題

來源:互聯網
上載者:User

標籤: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 ; }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.