尽量不要使用类型和数目不确定的参数。
C 标准库函数 printf 是采用不确定参数的典型代表,其原型为: int printf(const chat *format[, argument]…); 这种风格的函数在编译时丧失了严格的类型安全检查。
1 #include <iostream> 2 #include <stdlib.h> 3 #include <time.h> 4 5 using namespace std; 6 //定义产生[n1,n2]范围int随机数的函数 7 int rand(int n1,int n2) { 8 if (n1>n2) return -1; 9 if (n1==n2) return 0; 10 int temp=n1+int((n2-n1)*double(rand())/RAND_MAX); 11 return temp; 12 } 13 14 //main()函数的定义,加法练习程序 15 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 16 17 int main(int argc, char** argv) { 18 int i; 19 20 //使用当前的系统时间初始化随机数种子 21 srand( (unsigned)time( NULL ) ); 22 23 //加法练习 24 int a,b,c; 25 do { 26 a=rand(0,20); 27 b=rand(0,20); 28 L1: cout<<a<<"+"<<b<<"="; 29 cin>>c; 30 if (c==0) break; 31 if (c!=a+b) { 32 cout<<"Error! Try again!"<<endl; 33 goto L1; 34 } 35 cout<<"OK!"<<endl; 36 } while (1); 37 38 return 0; 39 }