printf的附加打印字符* [英] printf prints additional * character

查看:130
本文介绍了printf的附加打印字符*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的code,将大写小写:

I have a very simple code to convert Upper case to lower case:

#include <stdio.h>
int main()
{
char c;
int i=0;
for (i=0;i<10;i++){
    c=getchar();
    c=c-'A'+'a';
    printf("%c\n",c );
    }
return 0;
}

但运行这个简单的code总是我在输出额外的 * 字符。它打印由 * 继字符。请看下图:

But running this simple code always I have an additional * character at output. It prints the char following by a *. Take a look:

D
d
*
D
d
*
E
e
*

在哪里这是从哪里来的?

Where does this come from?

推荐答案

每个之后的输入的,pssed由于<大骨节病> ENTER 键$ P $,有一个换行存储在输入缓冲器,并在下次迭代阅读的getchar()

After each input, due to ENTER key pressed, there's a newline that is stored in the input buffer and read in the next iteration by getchar().

换行 \\ n )具有ASCII值 10 (十进制),添加到'A' - 'A'这是32(十进制),产生42(十进制),它打印的 *

a newline (\n) has ASCII value of 10 (decimal), added to the 'a'-'A' which is 32 (decimal), produces 42 (decimal), which prints the *.

FWIW,的getchar()返回 INT 。这是一个非常糟糕的主意来存储的getchar的返回值()字符变量,如在的情况下, 的getchar()失败,则可能返回值之一,例如 EOF 将不会被装修成一个字符型,甚至debuggin尝试进一步条件检查导致问题。更改

FWIW, getchar() returns an int. It's a very bad idea to store the return value of getchar() into a char variable, as, in case, getchar() fails, one of the possible return values, for example EOF will not be fitting into a char type, causing issues in further conditional check of even debuggin attempt. Change

char c;

int c = 0;

这篇关于printf的附加打印字符*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆