这只是一个简单的代码 我的代码是:
#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中也遇到了这个问题.
添加:现在我可以确定这是剪贴板工具的问题.这篇文章的结尾