c 語言學習第二天
阿新 • • 發佈:2021-06-25
常量
字串常量
- 字元
例如:'f'
,'i'
,'z'
,'a'
編譯器為每個字元分配空間。
'f' | 'i' | 'z' | 'a' |
---|
- 字串
例如:"hello"
編譯器為字串裡的每個字元分配空間以'\0'
結束。
'h' | 'e' | 'l' | 'l' | 'o' | '\0' |
---|
基本型別
- 整數型:
short int
,int
,long int
,long long int
(c99 新增).佔位元組大小按照從小到大書寫。 - 浮點數型別:
float
,double
,long double
- 字元型別:
char
- 布林型別:
_Bool
- 列舉型別:
enum
sizeof 運算子
使用者獲取資料型別或表示式的長度。
- sizeof(object); // sizeof(物件);
- sizeof(type_name); // sizeof(型別);
- sizeof object; // sizeof 物件;
因為根據編譯器不同,以下的資料型別佔位元組大小僅供參考
#include <stdio.h> int main(){ int i; char j; float k; i=123; j='c'; k=3.123; printf("size of int is %d \n",sizeof(int)); printf("size of i is %d \n",sizeof i ); printf("size of char is %d \n",sizeof(char)); printf("size of j is %d \n",sizeof j ); printf("size of flaot is %d \n",sizeof(float)); printf("size of k is %d \n",sizeof k ); return 0; }
size of int is 4
size of i is 4
size of char is 1
size of j is 1
size of flaot is 4
size of k is 4
#include<stdio.h> int main(){ printf("int sizeof is %d\n",sizeof(int)); printf("short int size is %d\n",sizeof(short)); printf("long int size is %d\n",sizeof(long)); printf("long long int size is %d\n",sizeof(long long)); printf("char size is %d\n",sizeof(char)); printf("_Bool size is %d\n",sizeof(_Bool)); printf("float size is %d\n",sizeof(float)); printf("double size is %d\n",sizeof(double)); printf("long double size is %d\n",sizeof(long double)); return 0; }
int sizeof is 4
short int size is 2
long int size is 8
long long int size is 8
char size is 1
_Bool size is 1
float size is 4
double size is 8
long double size is 16
signed 和 unsigned
signed:代表帶符號位
unsigned:代表不帶符號位 ≥0
#include<stdio.h>
int main(){
short i;
unsigned short j;
i = -1;
j = -1;
printf("i is %d\n",i);
printf("j is %u\n",j);
return 0;
}
i is -1
j is 65535
計算機資料單位
cup 讀懂的最小單位:bit(位元位).
bit(位元位) => b
1 / 0 |
---|
Byte(位元組) => B
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
---|
關係:1B = 8b
1 位元組可以表示多大的數? 十進位制:255
,十六進位制:FF
最大值計算方式:2的n次方-1
二進位制 | 十進位制 | 十六進位制 |
---|---|---|
0 | 0 | 0 |
1 | 1 | 1 |
10 | 2 | 2 |
11 | 3 | 3 |
100 | 4 | 4 |
101 | 5 | 5 |
110 | 6 | 6 |
111 | 7 | 7 |
1000 | 8 | 8 |
1001 | 9 | 9 |
1010 | 10 | A |
1011 | 11 | B |
1100 | 12 | C |
1101 | 13 | D |
1110 | 14 | E |
1111 | 15 | F |
10000 | 16 | 10 |
10001 | 17 | 11 |
... | ... | ... |
11111111 | 255 | FF |
- 計算
已知int
型別為 4 個位元組。
#include<stdio.h>
#include<math.h>
int main(){
int result = pow(2,32)-1;
printf("result is %d\n",result);
return 0;
}
test6.c: In function ‘main’:
test6.c:4:2: warning: overflow in implicit constant conversion [-Woverflow]
int result = pow(2,32)-1;
^
result is 2147483647
執行報出警告,超出定義範圍。為什麼會這樣?
因為在定義的
int
型別的時候預設會在前面加上signed
型別,所以左邊的第一位用來表示符號位。如果為 0 表示正數,如果為 1 表示負數。所以int result
其實只有 7 位用來表示數值,其最大值為2^(4*8-1) -1
:2,147,483,647.
修改如下:
#include<stdio.h>
#include<math.h>
int main(){
unsigned int result = pow(2,32) - 1; // 在int前加上unsigned
printf("result is %u\n",result); // 這裡的%d 需要改成%u
return 0;
}
[root@localhost day1]$ gcc test6.c && ./a.out
result is 4294967295
result 正常顯示為 2^8-1
: 4294967295.
計算機如何儲存數值?
採用補碼的形式儲存。
- 正數的補碼:該數的二進位制形式。
- 負數的補碼:
- 先取得該數的絕對值的二進位制,也就是正數的二進位制。
- 將第一步的值按位取反。
- 將第二步的值 +1.
例如:
7:
0 |
0 | 0 | 0 | 0 | 1 | 1 | 1 |
---|
-7:
- 獲得 7 的二進位制
0 |
0 | 0 | 0 | 0 | 1 | 1 | 1 |
---|
- 按位取反
1 |
1 | 1 | 1 | 1 | 0 | 0 | 0 |
---|
- +1
1 |
1 | 1 | 1 | 1 | 0 | 0 | 1 |
---|
總結
當左邊第一位為0
的時候,後面的1
越多,字面量值越大。
當左邊第一位為1
的時候,後面的0
越多,字面量值越大。
- 基本資料型別取值範圍
有符號 1 位元組 => -2^(1x8-1) ~ 2^(1x8-1)-1
無符號 1 位元組 => 0 ~ 2^(1x8)-1
有符號 4 位元組 => -2^(4x8-1) ~ 2^(4x8-1)-1
無符號 4 位元組 => 0 ~ 2^(4x8)-1
以此類推
為什麼採用補碼?
參考一下連結: