#include <stdio.h> int main() { int count1 =1; do{ int count2 =0; ++count2; printf(" count1=%d count2=%d",count1,count2); count1++; }while (count1<=8); printf(" count1=%d ",count1); return 0; }
不等价于下面这个,尽管两个输出一样
#include <stdio.h> int main() { int count1 =1; do{ int count2 =0; ++count2; printf(" count1=%d count2=%d",count1,count2); }while (++count1<=8); printf(" count1=%d ",count1); return 0; }
详情见下面这个例子
#include <stdio.h> int main(void) { int count= 0; do{ int count =0; ++count ; printf(" count=%d",count ); } while (++count<=8); printf(" count =%d ",count ); return 0; } /* count=0 count=0 count=1 dayin count=1<=8 count=0 count=1 dayin !! count=2<=8 ... count=0 count=1 dayin count=8<=8 count=0 count=1 dayin count=9 wo tiao count=9 shuchu 关键在于while 中的count 和do while 里面的不是一回事,while 使用的是那个外面的,do{}的count出了括号就死了 作用域就是这么回事,以大括号为界限,出来就死。 生存期就是,就活一轮,下一轮就重生 */
#include <stdio.h> float average(float x ,float y) { return (x+y)/2.0f; } int main (void) { float value1=0.0F; float value2=0.0F; float value3=0.0F; printf("Enter two floating-point values separated by blanks: "); scanf("%f %f",&value1,&value2); value3=average (value1,value2); printf(" The average is:%f ",value3); return 0; }/* 函数调用例子*/
#include <stdio.h> float average(float x ,float y);/* 也可以写做 float average (float ,float); */ int main (void) { float value1=0.0F; float value2=0.0F; float value3=0.0F; printf("Enter two floating-point values separated by blanks: "); scanf("%f %f",&value1,&value2); value3=average (value1,value2); printf(" The average is:%f ",value3); return 0; } float average(float x ,float y) { return (x+y)/2.0f; }
/*不使用指针和使用指针的比较 相当于制表符移动8个光标,等价于一个Tab键 不使用指针改变的不多 */ #include <stdio.h> int change(int number); int main (void) { int number =10; int result=0; result=change(number); printf(" In main ,result=%d, number=%d",result,number); return 0; } int change(int number) { number = 2*number; printf(" In function change ,number =%d ",number); return number; }
/* 使用指针,把number也改变了,因为指针就是他的地址,地址变了,啥都变了,把指针作为变元,若要对一个数进行操作,那么对他的指针操作,把他的指针当做变元,改变的他的指针才可以真正的改变这个数 */ #include <stdio.h> int change (int *pnumber); int main(void) { int number=10; int * pnumber=&number; int result =0; result =change(pnumber); printf(" In main, result =%d number=%d",result ,number ); return 0; } int change(int *pnumber) { *pnumber*=2; printf(" In function change ,*pnumber=%d ",*pnumber); return *pnumber ; }