scanf() 在 printf() 之后没有回车 return
scanf() no carriage return after printf()
我有以下代码从用户输入中读取 4 个数字:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMS 4
int main()
{
int nums[MAX_NUMS];
int i;
int num;
for (i = 0; i < MAX_NUMS; i++)
{
do
{
printf("Enter a value between 1 and 9: ");
scanf("%i",&num);
}
while(num > 9 || num < 1);
nums[i] = num;
}
return 0;
}
当我 运行 它时,我得到以下输出:
user@USER:/cygdrive/d
$ ./test.exe
Enter a value between 1 and 9: 1
Enter a value between 1 and 9: 2
Enter a value between 1 and 9: 3
Enter a value between 1 and 9: 4
user@USER:/cygdrive/d
$
为什么我输入数字后回车没有回车return?我错过了什么吗?
我在 cygwin 上:
user@USER:/cygdrive/d
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-2.x86_64/src/gcc-9.3.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-2.x86_64/src/gcc-9.3.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 9.3.0 (GCC)
user@USER:/cygdrive/d
$ uname -a
CYGWIN_NT-10.0 STEPAN 3.1.5(0.340/5/3) 2020-06-01 08:59 x86_64 Cygwin
user@USER:/cygdrive/d
$ echo $TERM
xterm-256color
在您的特定环境中,您按下确认输入的 Enter 键似乎被解释为原始 '\n'
。这个字符的本义,其实就是line feed,意思是“换行不回车return”( '\r'
).
在大多数终端中,回车键将 \r
和 \n
的效果相加,而您的情况不会发生这种情况。
要解决此问题,请尝试在输入插入后手动插入 '\r'
:
do
{
printf("Enter a value between 1 and 9: ");
scanf("%i",&num);
printf("\r");
}
while(num > 9 || num < 1);
我有以下代码从用户输入中读取 4 个数字:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMS 4
int main()
{
int nums[MAX_NUMS];
int i;
int num;
for (i = 0; i < MAX_NUMS; i++)
{
do
{
printf("Enter a value between 1 and 9: ");
scanf("%i",&num);
}
while(num > 9 || num < 1);
nums[i] = num;
}
return 0;
}
当我 运行 它时,我得到以下输出:
user@USER:/cygdrive/d
$ ./test.exe
Enter a value between 1 and 9: 1
Enter a value between 1 and 9: 2
Enter a value between 1 and 9: 3
Enter a value between 1 and 9: 4
user@USER:/cygdrive/d
$
为什么我输入数字后回车没有回车return?我错过了什么吗?
我在 cygwin 上:
user@USER:/cygdrive/d
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-2.x86_64/src/gcc-9.3.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-2.x86_64/src/gcc-9.3.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 9.3.0 (GCC)
user@USER:/cygdrive/d
$ uname -a
CYGWIN_NT-10.0 STEPAN 3.1.5(0.340/5/3) 2020-06-01 08:59 x86_64 Cygwin
user@USER:/cygdrive/d
$ echo $TERM
xterm-256color
在您的特定环境中,您按下确认输入的 Enter 键似乎被解释为原始 '\n'
。这个字符的本义,其实就是line feed,意思是“换行不回车return”( '\r'
).
在大多数终端中,回车键将 \r
和 \n
的效果相加,而您的情况不会发生这种情况。
要解决此问题,请尝试在输入插入后手动插入 '\r'
:
do
{
printf("Enter a value between 1 and 9: ");
scanf("%i",&num);
printf("\r");
}
while(num > 9 || num < 1);