#include<stdio.h>
#include<c开发者_Go百科onio.h>
#include<iostream.h>
void main()
{
int wh=1,i,j;
int sale[5][3];
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
sale[i][j]=0;
}
}
printf("%d",wh);
getch();
}
Certainly you should ditch Turbo C, if that is what you are using - get Code::Blocks from http://forums.codeblocks.org.
Your error is:
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
should be:
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
Arrays in C are indexed starting from zero. So an array:
int a[5];
has 5 elements:
a[0], a[1], a[2], a[3], a[4]
Note it does NOT have an element a[5]. Any attempt to access a[5] (or a[6] etc.) leads to what the C Standard calls "undefined behaviour" - your program is in an unknown state, from which it can never recover, and could do anything.
Code has errors. The array is int sale[5][3];
. That means that the index ranges spans 0 to 4
in the first dimension and 0 to 2
in the second dimension. The index in C starts from 0
. so the loop should be:
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
sale[i][j]=0;
}
}
Otherwise in your code you are accessing some memory location which you should not (beyond the array.
Although this should not make the compiler stop you from generating the executable.
Standards say using void main() is wrong. Some compilers will give errors if you try that.
精彩评论