帮忙看一个低级的c语言程序,为什么加上”printf("相同的有");“就会报错,而去掉就没问题了呢

#include <stdio.h>

main()
{
char a1[]="aaa";
char a2[]="dddddd";
printf("相同的有");//出错的地方??????
char *p1=a1,*p2=a2;
int i=same(p1,p2);
printf("%d",i);
}
int same(char *s1,char *s2)
{
int counter=0;
while(*s1&&*s2)
{
if(*s1==*s2)
{
counter++;
}
s1++;
s2++;
}

return counter;
}

第1个回答  2013-02-28
楼主你好!
你的int same(char *s1,char *s2)函数应该在
#include <stdio.h>下面声明吧!
要不你把整个int same(char *s1,char *s2)函数放到main()之前吧!
希望我的回答对你有帮助!
第2个回答  2013-02-28
与所使用的编译器有关,因为不同的编译器对“C标准”的支持也不同,例如如果编译器支持“C99”标准的话,“变量声明就不必放在语句块的开头”。
第3个回答  2013-02-28
所有变量都定义完了再写printf就好了,也就是说把char *p1=a1,*p2=a2;挪到printf之前
第4个回答  2013-02-28
把所以的定义或者声明放在其他的语句或函数调用之前,另外你的函数调用之前要声明:

#include <stdio.h>

main()
{
char a1[]="aaa";
char a2[]="dddddd";
int same(char *,char *);
int i;

char *p1=a1,*p2=a2;
i=same(p1,p2);
printf("相同的有");
printf("%d\n",i);
return 0;
}
int same(char *s1,char *s2)
{
int counter=0;
while(*s1&&*s2)
{
if(*s1==*s2)
{
counter++;
}
s1++;
s2++;
}

return counter;
}本回答被提问者采纳
第5个回答  2013-02-28
函数没申明……
相似回答