当前位置:编程学习 > C/C++ >>

这个c语言的程序编译没问题,执行时候会出错? 怎么回事?

程序:#include <stdio.h> void main () { int muban[3][3]={{1,2,1},{2,4,2},{1,2,1}}; int i,j,ii,jj; int sum1=0; for (i=0;i<3;i++) for (j=0;j<3;j++) sum1+=muban[i][j]; printf ("sum is %f \n",sum1); for (ii=0;ii<=2;ii++) { for ( jj=0;jj<=2;jj++) { printf(" %d ",muban[ii][jj]); } printf ("\n"); } }
答案:printf ("sum is %f \n",sum1);
//改为:%f改%d
printf ("sum is %d \n",sum1);
其他:应该没错啊 #include <stdio.h>
void main ()

{
	int muban[3][3]={{1,2,1},{2,4,2},{1,2,1}};
	int i,j,ii,jj;
	int sum1=0;
	for (i=0;i<3;i++)
	{	for (j=0;j<3;j++)
		{
			sum1+=muban[i][j];
		}
                //printf ("sum is %f \n",sum1);//你的输出类型弄错了,整数用%d
		printf ("sum is %d \n",sum1);
	}
	for (ii=0;ii<=2;ii++)
	{	for ( jj=0;jj<=2;jj++)
		{	printf(" %d ",muban[ii][jj]);
		}	
		printf ("\n");
	}


} for (i=0;i<3;i++)
{   for (j=0;j<3;j++)
	{
            sum1+=muban[i][j];  
         } 
}
要不试试看看 应该是printf ("sum is %f \n",sum1); 破坏了程序的完整性.导致程序出错 
可以修改为printf ("sum is %f \n",(double)sum1); 
或者printf ("sum is %d\n",(double)sum1); 数据类型你最好好好看一下,%d%f%s printf ("sum is %f \n",sum1);// 这是浮点型的
printf ("sum is %d \n",sum1);// 这是整型的

上一个:郝斌C语言的课件发我一份吧,万分感谢! justemem@gmail.com
下一个:关于C语言的问题!~~求解!!!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,