C 为什么for循环不是';t正在执行?(线性搜索)

C 为什么for循环不是';t正在执行?(线性搜索),c,linear-search,C,Linear Search,在这段线性搜索代码中,在执行第8行之后,它跳到第13行,而不执行for循环 输出: 输入数组的大小 5. 输入元素 输入要搜索的元素 代码如下: #include<stdio.h> int main() { int array[100],search,c,n; printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the elements:\n"); for(c=0

在这段线性搜索代码中,在执行第8行之后,它跳到第13行,而不执行for循环

输出: 输入数组的大小 5. 输入元素 输入要搜索的元素

代码如下:

#include<stdio.h>

int main()
{
   int array[100],search,c,n;
   printf("Enter the size of array\n");
   scanf("%d",&n);
   printf("Enter the elements:\n");
   for(c=0; c>n; c++)
   { 
       scanf("%d",&array[c]);
   }
   printf("Enter the no. to be searched\n");
   scanf("%d",&search);
   for(c = 0; c>n; c++)
   {
       if(array[c] == search)
       {
           printf("%d is at location %d\n",search,c);
           break;
       }    
       if(c == n)
           printf("Item not found\n");    
   }    
   return 0;
}





#包括
int main()
{
int数组[100],搜索,c,n;
printf(“输入数组的大小\n”);
scanf(“%d”和“&n”);
printf(“输入元素:\n”);
对于(c=0;c>n;c++)
{ 
scanf(“%d”和数组[c]);
}
printf(“输入要搜索的编号\n”);
scanf(“%d”,搜索(&S);
对于(c=0;c>n;c++)
{
if(数组[c]==搜索)
{
printf(“%d位于位置%d\n”,搜索,c);
打破
}    
如果(c==n)
printf(“未找到项”);
}    
返回0;
}

由于条件
c>n
,您没有进入for循环,请尝试将其更改为
c

for(c=0; c < n; c++)
(c=0;c
将条件更改为
cn
从一开始就是false。否则,在程序输出时输入一个负数xd:
printf(“输入数组大小\n”),您到底输入了什么?如果代码要检查函数(如:
scanf()
)返回的值(而不是参数值),则程序会告诉您很多信息,例如:
for(c=0;c>n;c++)
第二个参数表示代码何时留在循环中,而不是何时退出循环。建议:
用于(c=0;c