標籤:des style blog color io ar 使用 for sp
上課已經是第十一天,C語言的文法差不多要結束了。剩下的就只有掃尾操作。C語言一共有32個關鍵字。老劉先是列出這些關鍵字,然後講平時沒有講過的。
C語言關鍵字有:void int char short long double float unsigned signed
if else do while sizeof auto register const static enum
typedef return break goto continue swich struct union
case default extern for volatile
extern : 不分配記憶體,僅僅聲明為外部變數。
auto 經常變的變數 基本不用
register 寄存器變數 用於經常訪問的資料 加快訪問速度,但是數量有限。
volatile 說明變數與硬體相關,不希望被編譯器最佳化
typedef 對變數進行重新命名
const 唯讀變數 一般用於指標。
1 #include<stdio.h> 2 3 int main() 4 { 5 int i = 10; 6 int j = 20; 7 8 const int * p1 = &i; 9 int * const p2 = &i;10 11 *p1 =20;// err12 p1 = &j;//ok 13 *p1 = 21;//err 14 *p2 = 20 ; // ok15 p2 = &j ; err 16 }
const 往後面離誰最近就修飾誰,比如第八行,從const 離* 近,就表明*p裡面的內容不可變。第八行也可以寫成int const * p1 = &i; const 一般作為函數參數使用。
1 #include<stdio.h> 2 #include<assert.h> 3 4 int mystrcpy(char *dest ,const char * source ) 5 { 6 assert(source != NULL);//斷言 7 while(*source) 8 *dest++ = *source++; 9 }10 11 int main()12 {13 char *p = "hello world ";14 char *p2 = NULL;15 char data[1023]={0};16 17 mystrcpy(data,p2);18 printf("data is %s\n",data);19 20 21 }
注意這裡要使用斷言。
void *p 不知道存什麼類型的地址。一般是通過隱式類型轉換使用
宏定義:#define 僅僅是進行簡單的替換。所以盡量用()
為了防止標頭檔被重複包含,使用 #ifndef A #define A #endif 為了讓代碼和平台無關 使用 #ifdef #else #endif
為了注釋代碼 使用 #if 0/1 #else #endif
1 #include<stdio.h> 2 #include"bunfly.h" 3 #include"hello.h" 4 5 #if 1 6 int main() 7 { 8 printf("i am main\n"); 9 hello(); 10 bunfly();11 }12 #else 13 int main()14 {15 printf("hello world \n");16 17 }18 #endif
下午的時候老李給我們講面試技巧,類比面試了兩個人。感覺我現在去面試沒有一點優勢。還是要加強溝通技巧。周一還要交簡曆。
第十一天:C基礎之關鍵字