为什么我的代码工作在第 21 行 scanf 函数在第 18 行 printf 函数之前执行?
#include<stdio.h>
int main(){
int num, i, n, a;
printf("Enter total numbers of integers you want to enter: ");
scanf("%d", &n);
printf("\nEnter the numbers in the array : ");
int arr[n];
for(i = 0; i<n; i++){
scanf("%d ",&arr[i]);
}
printf("\n\n\nNow enter the number to searched in the array :");
scanf("%d", &num);
for(int j = 0; j<n; j++){
a = arr[j];
if(a == num)
{
printf("Your position for the number %d in the array is : %d",num, j+1);
break;
}
else
continue;
}
if( a!= num){
printf("Number not found!");
}
}
当我的 printf 在 scanf 之前编写时,为什么我的代码不能逐行运行。
#include<stdio.h>
int main(){
int num, i, n, a;
printf("Enter total numbers of integers you want to enter: ");
scanf("%d", &n);
printf("\nEnter the numbers in the array : ");
int arr[n];
for(i = 0; i<n; i++){
scanf("%d ",&arr[i]);
}
printf("\n\n\nNow enter the number to searched in the array :");
scanf("%d", &num);
for(int j = 0; j<n; j++){
a = arr[j];
if(a == num)
{
printf("Your position for the number %d in the array is : %d",num, j+1);
break;
}
else
continue;
}
if( a!= num){
printf("Number not found!");
}
}
Why is my code not working line by line, when my printf is written before scanf.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除 scanf 中用于 for 循环获取输入的额外空格即可。
由于该空间,编译器正在寻找输入并跳过打印语句并直接跳转到另一行。
请使用下面的代码片段。
请从我的系统中找到以下输出。
Just remove the extra space in scanf which was used in for loop to take input.
Because of that space compiler is looking for input and skipping your print statement and directly jumping to another line.
Please use the below snippet.
Please find my below output from my system.