7. 错误举例: int n;
scanf(“%d″,&n); /*在程序中临时输入数
组的大小 */
int a[n]; ① float a[0]; /* 数组大小为0没有意义 */
② int b(2)(3); /* 不能使用圆括号 */
③ int k, a[k]; /* 不能用变量说明数组大小*/ 数组说明中其他常见的错误
51. 程序如下:
#include
void main()
{
char string[81];
int i,num=0,word=0;
char c;
gets(string);
for (i=0;(c=string[i])!=′\ 0′;i++)
52. if(c==′ ′) word=0;
else if(word==0)
{ word=1;
num++;
}
printf(″There are %d words in the
line.\n″,num);
} 运行情况如下:
I am a boy.↙
There are 4 words in the line.
53. 例7.9 有3个字符串,要求找出其中最大者 程序如下:
#include
#include
void main ( )
{
char string[20];
char str[3][20];
int i;
for (i=0;i<3;i++)
gets (str[i]);
54. if (strcmp(str[0],str[1])>0)
strcpy(string,str[0])
else strcpy(string,str[1]);
if (strcmp(str[2],string)>0)
strcpy(string,str[2]);
printf(″\nthe largest string is∶
\n%s\n″,string);
}
55. 运行结果如下:
CHINA↙
HOLLAND↙
AMERICA↙
the largest string is∶
HOLLAND