这只是一个简单的代码 我的代码是:

#include <stdio.h>
int a[100];
int main(void) {
    for (int i = 0; i < 12; i++)
    {
        scanf("%d", &a[i]);
        printf("%d", a[i]);
    }
}

当我输入时: 0 1 0 2 1 0 1 3 2 1 2 1个 然后我得到了: 000000000000 如果我将其更改为:

#include <stdio.h>
int a[100];
int main(void) {
    for (int i = 0; i < 12; i++)
    {
        scanf("%d", &a[i]);
        getchar();       
        printf("%d", a[i]);
    }
}

当我输入时: 0 1 0 2 1 0 1 3 2 1 2 1个 然后我得到了: 001000201000 如果我将其更改为:

#include <stdio.h>
int a[100];
int main(void) {
    for (int i = 0; i < 12; i++)
    {
        scanf("%d ", &a[i]);
        getchar();
        getchar();
        printf("%d", a[i]);
    }
}

当我输入时: 0 1 0 2 1 0 1 3 2 1 2 1个 然后我得到了:

01021013212

最后一个yields 是我想要的,但我不明白为什么它会发生在两次得到字符之后. 我试着用浏览器找原因,但失败了. 我用的是m2的macbookpro,用的是适用于Mac的Clion,版本是Clion 2023.1.5. 工具链说生成工具是"ninja",而c编译器是"cc". 发信人:GCC

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

我只是程序的新手,开始使用剪辑.我不知道为什么它与我从书中学到的有所不同,也不知道我是否应该继续使用克里昂. 如果你能回答我的问题,我将不胜感激.? 我不知道生成的汇编代码是否意味着这一点,我按照在线方法获得了以下代码:

.section   __TEXT,__text,regular,pure_instructions
    .build_version macos, 13, 0    sdk_version 13, 3
    .globl _main                           ; -- Begin function main
    .p2align   2
_main:                                  ; @main
    .cfi_startproc
; %bb.0:
    sub    sp, sp, #48
    .cfi_def_cfa_offset 48
    stp    x29, x30, [sp, #32]             ; 16-byte Folded Spill
    add    x29, sp, #32
    .cfi_def_cfa w29, 16
    .cfi_offset w30, -8
    .cfi_offset w29, -16
    stur   wzr, [x29, #-4]
    stur   wzr, [x29, #-8]
    b  LBB0_1
LBB0_1:                                 ; =>This Inner Loop Header: Depth=1
    ldur   w8, [x29, #-8]
    subs   w8, w8, #12
    cset   w8, ge
    tbnz   w8, #0, LBB0_4
    b  LBB0_2
LBB0_2:                                 ;   in Loop: Header=BB0_1 Depth=1
    ldursw x9, [x29, #-8]
    adrp   x8, _a@PAGE
    add    x8, x8, _a@PAGEOFF
    str    x8, [sp, #16]                   ; 8-byte Folded Spill
    add    x8, x8, x9, lsl #2
    mov    x9, sp
    str    x8, [x9]
    adrp   x0, l_.str@PAGE
    add    x0, x0, l_.str@PAGEOFF
    bl _scanf
    bl _getchar
    bl _getchar
    ldr    x8, [sp, #16]                   ; 8-byte Folded Reload
    ldursw x9, [x29, #-8]
    ldr    w9, [x8, x9, lsl #2]
                                        ; implicit-def: $x8
    mov    x8, x9
    mov    x9, sp
    str    x8, [x9]
    adrp   x0, l_.str.1@PAGE
    add    x0, x0, l_.str.1@PAGEOFF
    bl _printf
    b  LBB0_3
LBB0_3:                                 ;   in Loop: Header=BB0_1 Depth=1
    ldur   w8, [x29, #-8]
    add    w8, w8, #1
    stur   w8, [x29, #-8]
    b  LBB0_1
LBB0_4:
    ldur   w0, [x29, #-4]
    ldp    x29, x30, [sp, #32]             ; 16-byte Folded Reload
    add    sp, sp, #48
    ret
    .cfi_endproc
                                        ; -- End function
    .globl _a                              ; @a
.zerofill __DATA,__common,_a,400,2
    .section   __TEXT,__cstring,cstring_literals
l_.str:                                 ; @.str
    .asciz "%d "

l_.str.1:                               ; @.str.1
    .asciz "%d"

.subsections_via_symbols

ADD:我使用Xcode得到了相同的结尾.也许这不是Clion的问题. 试试绒毛绒;

#include <stdio.h>
int a[100];
int main(void) {
    for (int i = 0; i < 12; i++)
    {
        scanf("%d ", &a[i]);
//        getchar();
//        getchar();
        printf("%d", a[i]);
        fflush(stdout);
    }
    printf("\n");
}

使用xcode.输出为:

0 1 0 2 1 0 1 3 2 1 2 1
000000000000
Program ended with exit code: 0
#include <stdio.h>
int a[100];
int main(void) {
    for (int i = 0; i < 12;) {
        if (scanf("%d", &a[i]) == 1) {
            printf("%d", a[i++]);
        } else {
            int ch = getchar();
            if (ch == EOF) break;
            printf("[\\x%x]", ch);
        }
        fflush(stdout);
    }
    printf("\n");
}

我试了试,我得到了:

0 1 0 2 1 0 1 3 2 1 2 1
0[\xc2][\xa0]1[\xc2][\xa0]0[\xc2][\xa0]2[\xc2][\xa0]1[\xc2][\xa0]0[\xc2][\xa0]1[\xc2][\xa0]3[\xc2][\xa0]2[\xc2][\xa0]1[\xc2][\xa0]2[\xc2][\xa0]1
Program ended with exit code: 0

Add:有时会发生,有时不会.这种重复发生了几次.最近一次发生在几分钟前.那次发生的情况如下:我使用来自相同副本的相同Num.第一次,它在Xcode或在线编码中有正常的输出.但当我在Clion中使用它时,我得到了一个错误的输出.如果我在Xcode中再次使用Num,输出也是错误的,副本中的Num变成了错误的数字.如果我试图从这个页面重新复制它,数字又恢复正常了.我不知道为什么会这样,但可能是个错误.

补充:错误似乎有时会发生.

Add:我怀疑这是因为我使用了一个名为Upaster的剪贴板工具,因为我在Xcode中也遇到了这个问题.

添加:现在我可以确定这是剪贴板工具的问题.这篇文章的结尾

推荐答案

我建议采用以下循环:

for (int i = 0; i < 12;) {
    if (scanf("%d", &a[i]) == 1) {
        printf("%d", a[i++]);
    } else {
        int ch = getchar();
        if (ch == EOF) break;
        printf("[\\x%x]", ch);
    }
    fflush(stdout);
}

这将显示您是否在输入中获得了干扰scanf的额外"不可见"字符


从您的下一篇帖子中可以明显看出,您的输入中包含这\xc2\xa0个字节对,而不是空格.这个两个字节的序列是Unicode无空格的UTF-8编码.通过使用utf-8语言环境,您可能能够让scanf忽略这些内容(可能只需在程序开始时添加#include <locale.h>和呼叫setlocale(LC_ALL, "")就足够了).或者,将您的输入更改为使用普通空格,而不是这些奇怪的空格.

C++相关问答推荐

如何用C(使用两个s补数算术的32位程序)计算

为什么海湾合作委员会在共享对象中的. init_data的虚拟内存地址之前留出一个空白

设计处理各种数据类型的方法和数据 struct

C如何显示字符串数组中的第一个字母

C/SDL程序,渲染不使用我的渲染器

fwrite无法写入满(非常大)缓冲区

将数据移动到寄存器时出现分段故障

对重叠字符串使用MemMove

从uint8_t*转换为char*可接受

关于scanf()和空格的问题

如何在C-函数中混合使用C代码和ASM?

For循环不会迭代所有字符串字符吗?(初学者问题)

搜索使用int代替time_t的用法

我应该在递归中使用全局变量吗

Go和C中的数据 struct 对齐差异

当我用scanf(&Q;%S%S%S&Q;,单词0,单词1,单词2)输入多个单词时,除了最后一个单词外,每个单词的第一个字符都丢失了

如何编写postgresql支持函数

cs50拼写器分配中的无限循环

为什么创建局部变量的指针需要过程在堆栈上分配空间?

C23 中的 [[reproducible]] 和 [[unsequenced]] 属性是什么?什么时候应该使用它们?