为什么我的代码工作在第 21 行 scanf 函数在第 18 行 printf 函数之前执行?

发布于 2025-01-10 07:56:21 字数 782 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2025-01-17 07:56:21

只需删除 scanf 中用于 for 循环获取输入的额外空格即可。

由于该空间,编译器正在寻找输入并跳过打印语句并直接跳转到另一行。

请使用下面的代码片段。

#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("Now 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!");
    }
}

请从我的系统中找到以下输出。

代码来自我的编译器

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.

#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("Now 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!");
    }
}

Please find my below output from my system.

code from my compiler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文