c - 在C中,Print语句没有在for循环中打印

标签 c function io

在我的程序中,在我输入第一个输入后。由于 scanf 旁边的 printf 语句,我预计会打印这些值,但这是第一次,它没有被打印。对于下一次迭代及其所有正常工作

例如:

输出:
输入输入10
12
i=0,a[i]=11896224
输入内容

代码:

#include<stdio.h>
#include<stdlib.h>
int func(int *, int);
int *a;
int main()
{
    int length = 5;
    a = (int *)malloc(sizeof(int)*length);
    for (int i = 0;i < length;i++)
    {
        printf("Enter input");
        scanf("%d\n", &a[i]);//100,104,108,112,116
        printf("i=%d,a[i]=%d\n", i, &a[i]);/*Print Statement which is not executing for first time*/
    }
    func(a, length);
    return 0;
}

int func(int *b, int length)
{
    printf("Length=%d", length);
    for (int j = 0;j < length;j++)
        printf("b[%d]=%d", j, b[j]);//
    return 0;
}

最佳答案

scanf 函数中删除 \n

关于c - 在C中,Print语句没有在for循环中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343528/

相关文章:

c - 在 C 中使用按位与隔离位

c - 如何计算 switch case 中的薪水?然后为不同的利率添加一个 if 语句?

c++ - 使用引用参数按引用传递意外行为

c - 从C中的文件中逐行读取

linux - Solaris 与 Linux 中的系统调用开销

将此 C 代码转换为 Delphi 代码

c - 使用先前分配的指针调用 strlen 时出现段错误

Javascript - 创建动态元素然后在元素单击时执行某些操作

performance - LLVM 中的参数转发

python - 由于不同的 "looping styles"导致不同的行为