Getchar只获取一次char

Getchar只获取一次char,c,C,即使while为真getchar也会迭代一次。我用getchar在中的whilecondition and body尝试了我的代码,但它不起作用 int main() { char* s = malloc(sizeof(char)) /*= get_string("Write number: ")*/; char a[MAXN]; int i = 0; do { a[i] = getchar(); *s++ = a[i];

即使
while
为真
getchar
也会迭代一次。我用
getchar
中的
while
condition and body尝试了我的代码,但它不起作用

int main() {
    char* s = malloc(sizeof(char)) /*= get_string("Write number: ")*/;
    char a[MAXN];
    int i = 0;
    do {
        a[i] = getchar();
        *s++ = a[i];
        i++;
    } while (isdigit(a[i-1]) && a[i-1] != EOF && a[i-1] != '\n' && i< MAXN);
    /*while (isdigit(*s++=getchar()))
        i++;*/
    *s = '\0';
    s -= i;
    long n = conversion(s);
    printf("\n%lu\n", n);
}
intmain(){
char*s=malloc(sizeof(char))/*=get_字符串(“写入编号:”)*/;
字符a[MAXN];
int i=0;
做{
a[i]=getchar();
*s++=a[i];
i++;
}while(isdigit(a[i-1])&&a[i-1]!=EOF&&a[i-1]!='\n'&&i
代码没有为malloc(sizeof(char))
分配足够的内存,因为它只有1个字节

当代码试图将第二个
字符
保存到
s
中时,可能会发生不好的事情:未定义的行为(UB)

在任何情况下,都不需要分配


而是形成一个合理的固定大小的缓冲区,并将字符/数字存储在那里

// The max digits in a `long` is about log10(LONG_MAX) + a few
// The number of [bits in an `long`]/3 is about log10(INT_MAX)
#define LONG_DEC_SZ (CHAR_BIT*sizeof(long)/3 + 3)

int main(void)  {
  char a[LONG_DEC_SZ * 2]; // lets go for 2x to allow some leading zeros

  int i = 0;
  int ch; // `getchar()` typically returns 257 different values, use `int`

  // As long as there is room and code is reading digits ...
  while (i < sizeof a && isdigit((ch = getchar())) ) {
    a[i++] = ch;
  }
  a[i++] = '\0';
  long n = conversion(a);
  printf("\n%ld\n", n);
}
//long'中的最大位数约为log10(long_max)+几个
//[long`]中的位数/3约为log10(INT_MAX)
#定义LONG_DEC_SZ(字符位*sizeof(LONG)/3+3)
内部主(空){
char a[LONG_DEC_SZ*2];//让我们用2x来允许一些前导零
int i=0;
int ch;//`getchar()`通常返回257个不同的值,使用`int`
//只要有空间和代码在读数字。。。
而(i


要做的事情:此代码不允许像
'-'
'+'

这样的前导符号字符,正如其他人指出的那样,
s
没有多大用处,因为
a
可以传递给
转换。同样,用于
s
malloc
只分配一个字节

在进行循环测试之前,您将递增
i
,因此必须在那里使用
i-1
。此外,循环结束时
i
太大

即使对于原始代码,也要执行
int chr=getchar();a[i]=chr和用
chr
替换
a[i-1]
可以简化一些事情

更好的是,通过重新构造使用
for
而不是
do/while
循环,我们可以为每个转义条件添加更多注释,而不是一个更大的单个条件表达式

#define MAXN        1000

int
main(void)
{
    char a[MAXN + 1];
    int i;

    for (i = 0;  i < MAXN;  ++i) {
        // get the next character
        int chr = getchar();

        // stop on EOF
        if (chr == EOF)
            break;

        // stop on newline
        if (chr == '\n')
            break;

        // stop on non-digit
        if (! isdigit(chr))
            break;

        // add digit to the output array
        a[i] = chr;
    }

    // add EOS terminator to string
    a[i] = 0;

    unsigned long n = conversion(a);
    printf("\n%lu\n",n);

    return 0;
}
#定义MAXN 1000
int
主(空)
{
字符a[MAXN+1];
int i;
对于(i=0;i
您到底按了哪些键作为输入?不要忘记这是一个键。
char*s=malloc(sizeof(char))
只适用于
字符串,因为
s
只有一个字节的内存。试试
malloc(MAXN+1)
你想用
*s++=a[i]实现什么因为s++在一次迭代后立即将您带出分配的内存范围,如上面的@chux注释所示。也许您的意思是声明
char*s=&a[0]在声明了
a
?@之后,当
a[i-1]<0
isdigit(a[i-1])
@chux-Ah时,除了学究式的UB之外,我还退休了一段不错的代码,我看错了。
EOF
也有同样的问题,但正如您所注意到的,除了数字检查之外,不需要检查它,否则会失败。:)