一、给多项选择使用else-if语句
1 if(salary<5000) 2 3 printf("yourpay is very poor."); 4 5 else if(salary<15000) 6 7 printf("Your pay is not good."); 8 9 else if(salary<50000) 10 11 printf("Your pay is very good."); 12 13 else 14 15 printf("Your pay is exceptional");
二、switch语句
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int val; 7 8 printf("请输入您要进入的楼层:"); 9 scanf("%d",&val); 10 11 switch(val) 12 { 13 case 1: 14 printf("1层开\n"); 15 break; 16 case 2: 17 printf("2层开\n"); 18 break; 19 case 3: 20 printf("3层开\n"); 21 break; 22 default: 23 printf("没有这一层\n"); 24 break; 25 } 26 system("pause"); 27 return 0; 28 }
1 switch(ticket_number) 2 3 { 4 5 case 35: 6 7 printf("Congratulations! You win first prize!"); 8 9 break; 10 11 case 122: 12 13 printf("You are in lick - second prize."); 14 15 case 78: 16 17 printf("You are in luck - third prize."); 18 19 break; 20 21 default: 22 23 printf("Too bad,you lose"); 24 25 }
三、for循环语句
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main (void) 4 { 5 6 printf("\n***********"); 7 for(int count=1;count<=8;++count) 8 printf("\n* *"); 9 printf("\n***********\n"); 10 system("pause"); 11 return 0; 12 }
1 #include<stdio.h> 2 int main(void) 3 { 4 long sum=0L; 5 int count=0; 6 int i=0; 7 printf("\nEnter the number of integers you want to sum:"); 8 scanf(" %d",&count); 9 for(i=1;i<=count;sum+=i++); 10 11 printf("\nTotal of the first %d numbers is %ld\n",count,sum); 12 system("pause"); 13 return 0; 14 }
1 #include<stdio.h> 2 #include<ctype.h> 3 int main(void) 4 { 5 char answer=0; 6 for(;;) 7 { 8 printf("Do you want to enter some more(y/n):"); 9 scanf("%c",&answer); 10 if(tolower(answer)=='n') 11 break; 12 } 13 system("pause"); 14 return 0; 15 }
1 #include<stdio.h> 2 #include<ctype.h> 3 int main(void) 4 { 5 char answer='N'; 6 double total=0.0; 7 double value=0.0; 8 int count=0; 9 printf("\nThis program calculates the average of""any number of values."); 10 for(;;) 11 { 12 printf("\nEnter a value"); 13 scanf(" %lf",&value); 14 total+=value; 15 ++count; 16 printf("Do you want to enter another value?(Y or N):"); 17 scanf(" %c",&answer); 18 if(tolower(answer)=='n') 19 break; 20 } 21 printf("\nThe average is %.2lf\n",total/count); 22 23 return 0; 24 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 int chosen=15; 6 int guess=0; 7 int count=3; 8 printf("\nThis is a guessing game."); 9 printf("\n I have chosen a nember between 1 and 20""which you must guess.\n"); 10 for(;count>0;--count) 11 { 12 printf("\nYou have %d tr%s left.",count,count==1?"y":"ies"); 13 printf("\nEnter a guess:"); 14 scanf("%d",&guess); 15 if(guess==chosen) 16 { 17 printf("\nYou guessed it!\n"); 18 return 0 ; 19 } 20 if(guess<1||guess>20) 21 printf("I said between 1 and 20.\n"); 22 else 23 printf("Sorry.%d id wrong.\n",guess); 24 } 25 printf("\nYou have had three tries an failed. The number was %d\n",chosen); 26 system("pause"); 27 return 0; 28 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int main(void) 5 { 6 do{int chosen=0; 7 int guess=0; 8 int count=3; 9 int limit=20; 10 srand((time(NULL))); 11 chosen=1+rand()%limit; 12 printf("\nThis is a guessing game."); 13 printf("\nI have chosen a number between 1 and 20""which you must guess.\n"); 14 for(;count>0;--count) 15 { 16 printf("\nYou have %d tr%s left.",count,count==1?"y":"ies"); 17 scanf("%d",&guess); 18 if(guess==chosen) 19 { 20 printf("\n You guessed it!\n"); 21 system("pause"); 22 return 0; 23 } 24 if(guess<1||guess>20) 25 printf("I said between 1 and 20.\n"); 26 else 27 printf("Sorry.%d is wrong.\n",guess); 28 } 29 printf("\nYou have had three tries and failed. The number was %ld\n",chosen); 30 }while(1); 31 system("pause"); 32 return 0; 33 34 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 long sum=0L; 6 int count=0; 7 int i=0; 8 int j=0; 9 printf("\nEnter the number of integers you want to sum:"); 10 scanf("%d",&count); 11 for(i=1;i<=count;i++) 12 { 13 sum=0L; 14 for(j=1;j<=i;j++) 15 sum+=j; 16 printf("\n%d\t%ld",i,sum); 17 } 18 system("pause"); 19 return 0; 20 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 long sum=1L; 6 int j=1; 7 int count=0; 8 int i=0; 9 printf("\nEnter the number of integers you want to sum:"); 10 scanf("%d",&count); 11 for(i=1;i<=count;i++) 12 { 13 sum=1L; 14 j=1; 15 printf("\n1"); 16 while(j<i) 17 { 18 sum+=++j; 19 printf("+%d",j); 20 } 21 printf("=%ld\n",sum); 22 } 23 system("pause"); 24 return 0; 25 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int a,b; 6 7 for(a=10; a>=1 && a<=10; --a) 8 { 9 for(b=1; b<=a; ++b) 10 { 11 printf("*"); 12 } 13 printf("\n"); 14 } 15 16 for(a=1; a<=10; ++a) 17 { 18 for(b=1; b<=a; ++b) 19 { 20 printf("*"); 21 } 22 printf("\n"); 23 } 24 system("pause"); 25 return 0; 26 }
四、do-while循环
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 int number=0; 6 int rebmun=0; 7 int temp=0; 8 printf("\nEnter a positive integer:"); 9 scanf("%d",&number); 10 temp=number; 11 do 12 { 13 rebmun=10*rebmun+temp%10; 14 temp=temp/10; 15 16 }while(temp); 17 printf("\nThe number %d reversed is %d rebumun ehT\n",number,rebmun); 18 system("pause"); 19 return 0; 20 }
五、数组
1 #include<stdio.h> 2 #include<stdbool.h> 3 int main(void) 4 { 5 char size[3][12]={ 6 {'6','6','6','6','7','7','7','7','7','7','7','7'}, 7 {'1','5','3','7',' ','1','1','3','1','5','3','7'}, 8 {'2','8','4','8',' ','8','4','8','2','8','4','8'} 9 }; 10 int headsize[12]={164,166,169,172,175,178,181,184,188,191,194,197}; 11 char *psize = *size; 12 int *pheadsize = headsize; 13 14 float cranium = 0.0; 15 16 int your_head = 0 ; 17 bool too_small = false ; 18 bool hat_found = false ; 19 int i = 0 ; 20 printf("\nEnter the circumference of your head above your eyebrows" 21 "in inches as a decimal value:"); 22 scanf("%f",&cranium); 23 your_head=(int)(8.0*cranium); 24 for(i = 1;i<12;i++) 25 { 26 if(your_head>*(pheadsize + i)) 27 continue; 28 if ( ( i == 0 ) && ( your_head < ( *pheadsize ) - 1 ) ) 29 { 30 printf("\nYou are the proverbial pinhead. No hat for" 31 "you I'm afraid.\n"); 32 too_small=true; 33 break; 34 } 35 if(your_head < *(pheadsize + i ) - 1 ) 36 i--; 37 printf("\nYOur hat size is %c %c %c %c\n", 38 *(psize + i ), 39 *( psize + 1 * 12 +i ), 40 (i == 4) ? ' ': '/'), 41 *(psize + 2 * 12 + i); 42 hat_found = true; 43 break; 44 } 45 if(!hat_found && !too_small) 46 printf("\nYou, in technical parlance, are a fathead." 47 "NO hat for you, I'm afraid.\n"); 48 49 system("pause"); 50 return 0; 51 }
1 #include<stdio.h> 2 #include<stdbool.h> 3 int main(void) 4 { 5 char size[3][12]={ 6 {'6','6','6','6','7','7','7','7','7','7','7','7'}, 7 {'1','5','3','7',' ','1','1','3','1','5','3','7'}, 8 {'2','8','4','8',' ','8','4','8','2','8','4','8'} 9 }; 10 int headsize[12]={164,166,169,172,175,178,181,184,188,191,194,197}; 11 float cranium=0.0; 12 int your_head=0; 13 int i=0; 14 bool hat_found=false; 15 printf("\nEnter the circumference of your head above your eyebrows" 16 "in inches as a decimal value:"); 17 scanf("%f",&cranium); 18 your_head=(int)(8.0*cranium); 19 for(i=1;i<12;i++) 20 if(your_head>headsize[i-1]&&your_head<=headsize[i]) 21 { 22 hat_found=true; 23 break; 24 } 25 if(your_head==headsize[0]) 26 { 27 i=0; 28 hat_found=true; 29 } 30 if(hat_found) 31 printf("\n Your hat size is %c %c %c %c\n", 32 size[0][i],size[1][i],(size[1][i]==' ')?' ':'/',size[2][i]); 33 else 34 { 35 if(your_head<headsize[0]) 36 printf("\nYou are the proverbial pinhead.No hat for""you I'm afraid.\n"); 37 38 39 else 40 printf("\nYou,in technical parlance,are a fathead.""No hat for you ,I'm afraid.\n"); 41 } 42 system("pause"); 43 return 0; 44 }
六、字符串
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 char str1[]="To be or not to be"; 6 char str2[]=",That is the question"; 7 int count=0; 8 while(str1[count]!='\0') 9 count++; 10 printf("\nThe length of the string \"%s\"is %d characters.",str1,count); 11 count=0; 12 while(str2[count]!='\0') 13 count++; 14 printf("\nThe length of the string\"%s\"is %d characters.\n",str2,count); 15 system("pause"); 16 return 0; 17 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 char str1[40] = "To be or not to be";//初始化字符的变量 6 char str2 [] = ",that is the question"; 7 int count1 = 0; 8 int count2 = 0; 9 while (str1[count1]) /*确定字符串的长度*/ 10 count1++; 11 while (str2[count2]) 12 count2++; 13 //检查str1是否有足够的空间容纳来那个字符串和终止字符 14 if (sizeof str1 < count1 + count2 + 1) 15 printf("\nYou can't put a quart into a pint pot."); 16 else 17 { 18 count2 = 0; 19 while((str1[count1++] = str2[count2++])); //把str2[count2]中的值赋予str1[count1]中 20 printf("\n%s\n",str1); 21 } 22 system("pause"); 23 return 0; 24 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int i = 0; 6 //声明一个二维char属猪,而不是声明两个一维数组; 7 char str[][40] = { 8 "To be or not to be", 9 ",that is the question" 10 }; 11 int count[] = {0,0}; 12 //可以在一次循环中确定两个字符串的长度: 13 for(i = 0; i < 2 ; i++) 14 while(str[i][count[i]]) 15 count[i]++; 16 if(sizeof str[0] < count[0] + count[1] + 1) 17 printf("\nYou can't put a quart into a pint pot."); 18 else 19 { 20 count[1] = 0; 21 while((str[0][count[0]++] = str[1][count[1]++])); 22 printf("\n%s\n",str[0]); 23 } 24 system("pause"); 25 return 0; 26 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define STR_LENGTH 40 //给数组的大小定义了一个符号 5 int main(void) 6 { 7 char str1[STR_LENGTH] = "To be or not to be"; 8 char str2[STR_LENGTH] = ",that is the question"; 9 if(STR_LENGTH > strlen(str1) + strlen(str2))//检查数组是否有足够的空间 10 printf("\n%s\n",strcat(str1,str2));//strcat()函数连接字符串 11 else 12 printf("\nYou can't put a quart into a pint pot"); 13 14 system("pause"); 15 return 0; 16 17 }
1 #include <stdio.h> //标准输入输出的头文件 2 #include <stdlib.h> 3 #include <string.h> //处理字符的头文件 4 int main(void) 5 { 6 //先声明两个字符数组 7 char word1[20]; //数组的大小设置为20 8 char word2[20]; 9 printf("\nType in the first word (less than 20 characters):\n 1:"); 10 scanf("%19s",word1); 11 printf("Type in the second word (less than 20 characters):\n 2:"); 12 scanf("%19s",word2); 13 14 if(strcmp(word1,word2)==0) 15 printf("You have entered identical words"); 16 else 17 printf("%s precedes %s",(strcmp(word1,word2)<0)?word1 : word2, 18 (strcmp(word1,word2)<0)?word2 : word1); 19 20 system("pause"); 21 return 0; 22 }
1 #include <stdio.h> //标准输入输出的头文件 2 #include <stdlib.h> 3 #include <string.h> //处理字符的头文件 4 int main(void) 5 { 6 char str[] = "The quick brown fox"; 7 char c = 'q'; 8 char *pGot_char = NULL; //初始化一个指针等于0 9 pGot_char = strchr(str,c);//在str字符串中查找c中的字符 10 if(pGot_char != NULL) 11 printf("Character found was %c\n",*pGot_char); 12 system("pause"); 13 return 0; 14 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int main(void) 5 { 6 7 char str1[] = "This string contains the holy grail."; 8 char str2[] = "the holy grail"; 9 char str3[] = "the holy grill"; 10 if (strstr(str1,str2) == NULL) 11 printf("\n\"%s\"was not found.",str2); 12 else 13 printf("\n\"%s\" was found in \" %s\"",str2,str1); 14 if(strstr(str1,str3) == NULL) 15 printf("\n\"%s\"was not found.",str3); 16 else 17 printf("\nWe shouldn't get to here!"); 18 system("pause"); 19 return 0; 20 21 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 char buffer[80]; 6 int i = 0; 7 int num_letters = 0; 8 int num_digits = 0; 9 printf("\nEnter an interesting string of less than 80 characters"); 10 gets(buffer);//用一下语句将字符串读入数组buffer; 11 while(buffer[i] != '\0' ) 12 { 13 if(isalpha(buffer[i])) 14 num_letters++; 15 if(isdigit(buffer[i++])) 16 num_digits++; 17 } 18 printf("\nYour string contained %d letters and %d digits.\n",num_digits); 19 20 21 system("pause"); 22 return 0; 23 }
1 /*使用函数toupper()和函数strstr()可以确定一个字符串是否出现在另一个字符串中忽略大小写*/ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <ctype.h> 6 int main(void) 7 { 8 char text[100]; 9 char substring[40]; 10 int i=0; 11 printf("\nEnter the string to be searched (less than 100 characters):\n"); 12 fgets(text,sizeof(text),stdin); //stdin文件流的读取函数 13 printf("\nEnter the string sought (less than 40 characters):\n"); 14 fgets(substring,sizeof(substring),stdin); 15 text[strlen(text)-1] = '\0'; //用\0覆盖\n 16 substring[strlen(substring)-1] = '\0'; 17 printf("\nFirst string entered:\n%s\n",text); 18 printf("\nSecond string entered:\n%s\n",substring); 19 for( i = 0 ; (text[i] = toupper(text[i])); i++); //将字符串转化成大写 20 for( i = 0 ; (substring[i] = toupper(substring[i])) ; i++); 21 //条件运算符根据strstr()函数是否返回Null,选择was not ,was作为输出字符串的一部分 22 printf("\nThe second string %s found in the first.\n",(( 23 strstr(text,substring) == NULL)? "was not" : "was")); 24 25 system("pause"); 26 return 0; 27 }
1 #include <wchar.h> 2 #include <stdio.h> 3 int main(void) 4 { 5 wchar_t text[100]; 6 wchar_t substring[40]; 7 int i = 0; 8 printf("\nEnter the string sought (less than 40 characters) : \n"); 9 fgetws(text,40,stdin); 10 printf("\nEnter the string sought (less than 40 characters) : \n"); 11 fgetws(substring,40,stdin); 12 13 text[wcslen(text) - 1] = L'\0'; 14 substring[wcslen(substring) - 1] = L'\0'; 15 16 printf("\nFirst string entered:\n%S\n", text ); 17 printf("\nSecond string entered:\n%S\n", substring); 18 19 for(i = 0 ; (text[i] = towupper(text[i])); i++); 20 for(i = 0 ; (substring[i] = towupper(substring[i])) ; i++); 21 22 printf("\nThe second string %s found in the first.", 23 ((wcsstr(text,substring) == NULL) ? "was not" : "was")); 24 system("pause"); 25 return 0; 26 }
七、指针
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int number = 0; //声明一个int型变量 6 int *pointer = NULL; //声明一个int型指针 7 number = 10; 8 printf("\nnumber's address: %p",&number); //输出number的地址 9 printf("\nnumber's value:%d\n\n",number); //输出number的值 10 pointer = &number; //使用寻址运算符&获取变量number的地址,将该地址存储到pointer中 11 printf("pointer's address: %p",&pointer); //输出pointer的地址 12 printf("\npointer's size: %d bytes",sizeof(pointer)); //输出pointer的字节 13 printf("\npointer's value: %p",pointer); //输出pointer的值 14 printf("\nbalue pointed to: %d\n",*pointer); //输出指向number的值 15 system("pause"); 16 return 0; 17 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 long num1 = 0L; 6 long num2 = 0L; 7 long *pnum = NULL; 8 9 pnum = &num1; /* Get address of num1 */ 10 *pnum = 2; /* set num1 to 2 */ 11 ++num2; /* Increment num2 */ 12 num2 += *pnum; 13 14 pnum = &num2; 15 ++*pnum; 16 17 printf("\nnum1 = %ld num2 = %ld *pnum = %ld *pnum + num2 = %ld\n",num1, 18 num2,*pnum,*pnum + num2); 19 system("pause"); 20 return 0; 21 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 int value = 0; 6 int *pvalue = NULL; 7 8 pvalue = &value; 9 printf("Input an integer:"); 10 scanf(" %d",pvalue); 11 printf("\nYou entered %d\n",value); 12 system("pause"); 13 return 0; 14 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 long multiple[] = "a string"; 6 7 char *p = multiple; 8 9 int i = 0; 10 for(i = 0 ; i < strlen(multiple) ; i++) 11 printf("\nmultiple[%d] = %c * (p +%d ) = %c &multile[%d] = %p p+%d = %p\n", 12 i,multiple[i], i, *(p+i), i, &multiple[i], i, p+i); 13 system("pause"); 14 return 0; 15 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 char multiple[] = "My string"; 6 7 char *p = &multiple[0]; 8 printf("\nThe address of the first array element : %p",p); 9 10 p = multiple; 11 printf("\n The address obtained from the array name:%p\n",p); 12 system("pause"); 13 return 0; 14 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 long multiple[] = {15L, 25L, 35L, 45L}; 6 long *p = multiple; 7 int i = 0; 8 for(i = 0 ; i < sizeof(multiple)/sizeof(multiple[0]) ; i++ ) 9 printf("\naddress p+%d (&multiple[%d]): %d * (p+%d) value : %d ", 10 i, i, p+i, i, *(p+i)); 11 printf("\n Type long occupies: %d bytes\n", sizeof(long)); 12 system("pause"); 13 return 0; 14 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 system("color 02"); 6 char board[3][3] = { 7 {'1','2','3'}, 8 {'4','5','6'}, 9 {'7','8','9'} 10 }; 11 printf("address of board : %p\n",board); 12 printf("address of board[0][0] : %p\n",&board[0][0]); 13 printf("but what is in board[0] : %p\n",board[0]); 14 system("pause"); 15 return 0; 16 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 char board[3][3] = { 6 {'1','2','3'}, 7 {'4','5','6'}, 8 {'7','8','9'} 9 }; 10 printf("value of board[0][0] : %c\n",board[0][0]); 11 printf("value of *board[0] : %c\n",*board[0]); 12 printf("value of **board : %c\n",**board); 13 14 system("pause"); 15 return 0; 16 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 int i = 0 ; 6 char board[3][3] = { 7 {'1','2','3'}, 8 {'4','5','6'}, 9 {'7','8','9'} 10 }; 11 for(i = 0 ; i < 9 ; i++) 12 printf("board : %c\n", *(*board + i)); 13 14 system("pause"); 15 return 0; 16 }
八、程序结构
1 #include <stdio.h> 2 #include <stdlib.h> 3 float average(float x,float y) 4 { 5 return (x + y)/2.0; 6 } 7 8 int main(void) 9 { 10 float value1 = 0.0F; 11 float value2 = 0.0F; 12 float value3 = 0.0F; 13 14 printf("Enter two floating-point values separated by blanks:\n"); 15 scanf("%f %f",&value1,&value2); 16 value3 = average(value1,value2); 17 printf("%f",value3); 18 system("pause"); 19 return 0; 20 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int change(int *pnumber); //修改函数change()的原型,把参数指定为一个指针 4 5 int main(void) 6 { 7 int number = 10; 8 int *pnumber = &number; 9 int result = 0; 10 11 result = change(pnumber); 12 printf("\nIn main,result = %d\tnumber = %d",result,number); 13 system("pause"); 14 return 0; 15 } 16 17 int change(int *pnumber) 18 { 19 *pnumber *= 2 ; 20 printf("\nIn function change,*pnumber =%d\n",*pnumber); 21 return *pnumber; 22 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int change(int number); 4 5 int main(void) 6 { 7 int number = 10; 8 9 int result = 0; 10 11 result = change(number); 12 printf("\nIn main,result = %d\tnumber = %d\n",result,number); 13 system("pause"); 14 return 0; 15 } 16 17 int change(int number) 18 { 19 number *= 2 ; 20 printf("\nIn function change,*pnumber =%d\n",number); 21 return number; 22 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int sum(int,int); 5 int product(int,int); 6 int difference(int,int); 7 8 int main(void) 9 { 10 int a = 10; 11 int b = 5; 12 int result = 0; 13 int (*pfun)(int,int); //声明一个函数指针 14 15 pfun = sum; //这个指针可以赋予任何带两个int参数且返回int值的函数。 16 result = pfun(a,b); 17 /*在次将指针名当成函数名来使用,后面跟随放在括号中的变元列表。 18 而将函数指针变量名当作原来的函数名,则变元列表必须对应函数头的参数列表*/ 19 printf("\npfun = sum result result = %d",result); 20 21 pfun = product; 22 result = pfun(a,b); 23 printf("\npfun = product result = %d",result); 24 25 pfun = difference; 26 result = pfun(a,b); 27 printf("\npfun = difference result = %d\n",result); 28 29 30 system("pause"); 31 return 0; 32 } 33 34 int sum(int x,int y) 35 { 36 return x + y; 37 } 38 39 int product(int x,int y) 40 { 41 return x * y; 42 } 43 44 int difference(int x,int y) 45 { 46 return x - y; 47 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int sum(int,int); 5 int product(int,int); 6 int difference(int,int); 7 8 int main(void) 9 { 10 int i = 0; 11 int a = 10; 12 int b = 5; 13 int result = 0; 14 int (*pfun[])(int,int) = { sum, product,difference}; //定义一个数组函数指针 15 16 for(i = 0; i < 3; i++) 17 { 18 result = pfun[i](a,b); 19 printf("\nresult = %d",result); 20 } 21 22 result = pfun[1](pfun[0](a,b),pfun[2](a,b)); 23 printf("\n\nThe product of the sum and the difference = %d\n"); 24 25 system("pause"); 26 return 0; 27 } 28 29 int sum(int x,int y) 30 { 31 return x + y; 32 } 33 34 int product(int x,int y) 35 { 36 return x * y; 37 } 38 39 int difference(int x,int y) 40 { 41 return x - y; 42 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int sum(int,int); 5 int product(int,int); 6 int difference(int,int); 7 //将函数指针作为变元的函数是any_function(),它的函数原型如下: 8 int any_function(int(*pfun)(int,int),int x,int y); 9 10 int main(void) 11 { 12 int i = 0; 13 int a = 10; 14 int b = 5; 15 int result = 0; 16 int (*pf)(int,int) = sum; //定义一个数组函数指针 17 18 result = any_function(pf,a,b); 19 printf("\nresult = %d",result); 20 result = any_function(product,a,b); 21 printf("\nresult = %d",result); 22 printf("\nresult = %d\n",any_function(difference,a,b)); 23 24 system("pause"); 25 return 0; 26 } 27 28 int any_function(int(*pfun)(int,int),int x,int y) 29 { 30 return pfun(x,y); 31 } 32 33 int sum(int x,int y) 34 { 35 return x + y; 36 } 37 38 int product(int x,int y) 39 { 40 return x * y; 41 } 42 43 int difference(int x,int y) 44 { 45 return x - y; 46 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void test1(void); 5 void test2(void); 6 7 int main(void) 8 { 9 int i = 0; 10 for(i = 0; i < 5; ++i) 11 { 12 test1(); 13 test2(); 14 } 15 system("pause"); 16 return 0; 17 } 18 void test1(void) 19 { 20 int count = 0; 21 /* 22 这是自动变量,所以它不会在程序开始执行时初始化。 23 如果不给它指定初始值,它将会含有一个垃圾值。这个变量 24 会在每次执行函数时初始化为0,在每次推出test1()后删除 25 ,因此它永远不会大于1 26 */ 27 printf("\ntest1 count = %d", ++count); 28 } 29 30 void test2(void) 31 { 32 static int count = 0; 33 /* 34 只要程序开始执行,静态变量就会一直存在, 35 但是它只能在声明它的范围内可见,不能再该作用域的外部引用。 36 */ 37 printf("\ntest2 const = %d",++count); 38 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int count = 0; 5 /* 6 这不是静态变量(也可以把它声明成静态变量),而是全局变量, 7 所以如果没有初始化它,它就默认为0。从声明该全局变量到程序 8 结束的任何函数中访问它。 9 */ 10 11 void test1(void); 12 void test2(void); 13 14 int main(void) 15 { 16 int count = 0; //这是自动变量,在main()函数中声明 17 for( ; count < 5; count++) 18 { 19 test1(); 20 test2(); 21 } 22 system("pause"); 23 return 0; 24 } 25 void test1(void) 26 { 27 printf("\ntest1 count = %d", ++count); 28 } 29 30 void test2(void) 31 { 32 static int count = 0; 33 34 printf("\ntest2 const = %d\n",++count); 35 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int count = 0; 5 /* 6 这不是静态变量(也可以把它声明成静态变量),而是全局变量, 7 所以如果没有初始化它,它就默认为0。从声明该全局变量到程序 8 结束的任何函数中访问它。 9 */ 10 11 void test1(void); 12 void test2(void); 13 14 int main(void) 15 { 16 int count = 0; //这是自动变量,在main()函数中声明 17 for( ; count < 5; count++) 18 { 19 test1(); 20 test2(); 21 } 22 system("pause"); 23 return 0; 24 } 25 void test1(void) 26 { 27 printf("\ntest1 count = %d", ++count); 28 } 29 30 void test2(void) 31 { 32 static int count = 0; 33 34 printf("\ntest2 const = %d\n",++count); 35 }
1 #include <stdio.h> 2 #include <stdarg.h> 3 4 double average(double v1,double v2,...); // Function prototype 5 6 int main(void) 7 { 8 double Val1 = 10.5, Val2 = 2.5; 9 int num1 = 6, num2 = 5; 10 long num3 = 12, num4 = 20; 11 12 printf("\n Average = %lf",average(Val1,3.5,Val2,4.5,0.0)); 13 printf("\n Average = %lf",average(1.0,2.0,4.0,5.0,6.0,7.0,8.0,9.0,0.0)); 14 printf("\n Average = %lf\n",average((double)num2,Val2,(double)num1, 15 (double)num4,(double)num3,0.0)); 16 17 system("pause"); 18 return 0; 19 } 20 21 double average( double v1,double v2,...) 22 { 23 va_list parg; // Pointer for variable argument list 24 double sum = v1 + v2; // Accumulate sum of the arguments 25 double value = 0; // Argument value 26 int count = 2; 27 28 va_start(parg,v2); // Initialize argument pointer 29 30 while((value =va_arg(parg,double)) != 0.0) 31 { 32 sum +=value; 33 count++; 34 } 35 va_end(parg); // End variable argument process 36 return sum/count; 37 }