/******************************** ** wzsts《C語言深度剖析》2016 ** ** ** ** fun1~fun6代表6章節內容 ** **fun10~fun19代表fun1所調用函數 ** ** ** ** 世界因規則而美好 ** ** #if(1)可運行,#if(0)不運行。 ** ** Data Author PC ** **16.1.1 wz VM.CentOS6.5 ** *********************************/ //第一章 以前打過,然後換系統丟失文件。想想,有些東西,失去了,可以換更好的!*/ #include <stdio.h> #include<string.h> #define N 10 /* 定義分配空間,而extern和define聲明,宏替換沒有分配空間 */ #define EPSINON 0.0001 fun0() /* 函數沒有類型,而返回值有類型 */ { int fx=10; return fx; } void static fun()/* 即便類型不同,也不能重名 */ { /* int fun =10 函數名,變量名不要重複 */ } void fun10() { int i=4; int *p=NULL; int a[10]={1,2,3,4,5}; char b[ ]="abcde"; char *c="abcde"; /* char *c[10]="abcde"; error*/ char d[ ]={"abcde"}; char e[]={'w','z','z','x'}; if(b!=NULL) if(NULL==c) ; else printf(" c isn't NULL\n"); else { NULL; /* 細節風格,防止誤認 */ } printf("*********************\n"); printf("%d\n",sizeof( p) ) ; /* 4 */ printf("%d\n",sizeof( *p) ) ; /* 4 */ printf("%d\n",sizeof( int)*i ) ;/* 4 i=0,is 0 i=4,is 16*/ printf("*********************\n"); printf("%d\n",sizeof( a) ) ; /* 40 */ printf("%d\n",sizeof( a[10] ) ) ; /* 4 */ printf("%d\n",sizeof( &a) ) ; /* 4 */ printf("%d\n",sizeof( &a[0]) ) ; /* 4 */ printf("%d,%d\n",sizeof( b),strlen(b) ) ; /* 6,5*/ printf("%d,%d\n",sizeof( c),strlen(c) ) ; /* 4,5*/ printf("%d,%d\n",sizeof( &c),strlen(e) ) ; /* 4,9*/ printf("%d,%d\n",sizeof( d),strlen(d) ) ; /* 6,5*/ printf("*********************\n"); } void fun11() { int i=-20; unsigned j=10; float ftestval=0.0,ff=0; if(ftestval==0.0) printf("test 1 \n"); if((ftestval>=-EPSINON)&&(ftestval<=EPSINON)) printf("test 2 \n"); ff=10000000.00+0.00000001; printf("test 2 is better \n f=%f d=%d\n",ff,ff); /* 不能如此轉化 */ i=(int)ff; printf( "f=%f d=%d\n",ff,i); /* 強制轉化 */ //bool b; C++ //if(b==FALSE) ; VC++ 中TRUE是1VB中TRUE是-1 //printf("d=%d,u=%u\n",i+j,i+j); /*d=-10,u=4294967286*/ //for(j=9;j>=0;j--) {printf("%u\n",j);} /*死循環 一直打印 */ //for(j=9;j>=1;j--) {printf("%u\n",j);} /* 修改爲打印9到1 */ scanf("%d\n",&i); switch(i) {/****** right ********/ case 1:printf("case 1\n"); case 2:printf("case 2\n"); /******* error *******/ // case A:printf("case A\n"); /* case語句 後應少於20行 */ // scanf("%d\n",&i); switch(i) { case 1:printf("case 2.1\n");break; case 2:printf("case 2.2\n");break; } default:printf(" error\n"); /* 可無,有則放默認錯誤程序 break有妙用 case 後是整型和字符型表達式 */ /********* error end **********/ } } void fun12() { /* for(初值循環條件;循環語句) for(i=0,j=1;i<10,j<3;i++,j++) while(循環條件){} do{} 程序至少執行一次 while(循環條件) */ static int i=0; //for(;i<10;i++) for(i=0;i<=10;i++) /* 半開半閉寫法 */ { ; } printf("***************\n"); while(i) { i--;printf("%d",i); } printf("%c\n",13); /* 需要記憶一些特別的ASCII碼值13 CR回車 */ printf("***************\n"); do {printf("%d\n",i);} /* 括號外不可放其他代碼 */ while(i); printf("%d\n",i); printf("***************\n"); /* **長循環在內效率高,程序不要修改循環變量的值 **循環要短,代碼清晰,3層以內 **死循環不一定死,也不一定沒有用;(測試機子可以開多少空間,跑認爲終止的程序) */ printf("c\n"); do { for(i=0;i<5;i++) { printf("*"); // if(i==1) goto wz; if(i==2) continue; /*continue終止 本次(本輪)循環*/ if(i==4) /*if(i=4)結果不一樣*/ break; /*break 終止 本層循環*/ } printf("%2d",i); wz: /*goto 建議少用跳出本層循環,也跳不出函數*/ printf("%d",i); printf("\n"); i-=4; /*輕易改的動數字則死循環打印*/ printf("%2d",i); } while(i>0); printf("\n"); printf("%d\n",i); } void fun13() { // extern int i; // static int i; // unsigned int i; // auto int i; // const int i; // long int i; // short int i; //volatile int i; struct{}su; printf("%d",sizeof(su));/* VC IS 1 ,VM IS 0;*/ typedef struct{int l;int a[0];}type; //typedef struct{int l;int a[ ];}type;/*可變長結構體 可變長數組,柔性數組,不同編譯器不同寫法*/ //typedef struct{int l;int a[10];}type; /* 不同的意思*/ enum EN{A=0,B,C=100,d,e}EN; union check{int i;char c;}un; un.i=1; printf("%d\n",un.c); /*01 0001->1000 小端存儲 */ printf("%d\n", C); printf("%d\n", sizeof(EN)); /* 是一個指針,32位機上爲4 */ /* 區別 枚舉 宏 —————————————————————————————————————— |編譯時 |確定值 |替換,未確定值| |定義常量 |一次多個 | 一次一個 | |編輯器調試 | 可以 | 不可以 | |定義函數 | 不可以 | 可以 | */ } void fun14() { auto int a=0; const int b=1; register c=2; int DataGotFromfun0=fun0(); printf("%d\n",a); // printf("%d\n",&a); //測試正確則如此註釋 printf("%d\n",printf("%d ",a)); printf("%d\n",printf("%d \n",a)); /* 0 ** 0 2 ** 0 ** 3 */ printf("%d\n",c); /* printf("%d\n",&c); 測試錯誤則如此註釋*/ /* ** static 修飾變量,函數,C++中 */ printf("DataGotFromfun0 is %d\n",DataGotFromfun0); /* ** 變量,函數 命名所涉及英語語法 */ printf("%d\n",b); /* b++; const==readonly */ printf("%d\n",sizeof(int) ) ;/* 4 */ printf("%d\n",sizeof( a ) ) ;/* 4 */ printf("%d\n",sizeof a ) ;/* 4 */ /* printf("%d\n",sizeof int ) ; */ /* 同return(a);括號可去 */ } void main() { //fun10(); //fun11(); //fun12(); //fun13(); //fun14(); }
[ 1011] <<C語言深度剖析>> 測試 TEST
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.