字元:
- char : -128 ~ 127
整數:
- int : -2147483648 ~ 2147483647
- short : -32768 ~ 32767
- long : -2147483648 ~ 2147483647
- long long
浮點數:
- float : 10^-38~10^38
- double : 10^-308~10^308
另外C語言的整數型態包含了正整數和負整數,可以使用 signed和unsigned分別設定設定整數型態為有正負號和無正負號,例如unsigned int為無正負號 ,因為原來的整數型態就有包含正負號了,因此不用在用signed進行設定,下面的範例是顯示資料型態所佔用的位元組大小。
#include<stdio.h>
int main(void) {
printf("%s%ld\n","char:",sizeof(char));
printf("%s%ld\n","int:",sizeof(int));
printf("%s%ld\n","short:",sizeof(short));
printf("%s%ld\n","long:",sizeof(long));
printf("%s%ld\n","long int:",sizeof(long int));
printf("%s%ld\n","long long:",sizeof(long long));
printf("%s%ld\n","float:",sizeof(float));
printf("%s%ld\n","double:",sizeof(double));
printf("%s%ld\n","unsigned int:",sizeof(unsigned int));
printf("%s%ld\n","unsigned char:",sizeof(unsigned char));
printf("%s%ld\n","unsigned short:",sizeof(unsigned short));
printf("%s%ld\n","unsigned long:",sizeof(unsigned long));
printf("%s%ld\n","unsigned long long:",sizeof(unsigned long long));
return 0;
}
C語言在輸入輸出中有對應的輸入輸出格式,資料型態都會有對應的輸入輸出格式:
- %d : 十進位整數
- %o : 八進位整數
- %x : 十六進位整數,超過10的數字以小寫表示,例如0xf
- %X : 十六進位整數,超過10的數字以大寫表示,例如0xF
- %u : 不帶符號的十進位整數
- %c : 輸出字元
- %s : 輸出字串
- %e : 使用科學記號,e為小寫
- %E : 使用科學記號,E為小寫
- %ld: 長整數十進位整數
- %f : 輸出浮點數
簡略的範例如下:
#include<stdio.h>
int main(void) {
printf("%d\n",10);
printf("%f\n",10.0);
printf("%c\n",'a');
return 0;
}
沒有留言:
張貼留言