简体   繁体   English

当我用 ASCII 十进制代码打印输入的字符时,会打印奇怪的 10 值

[英]Strange 10 value gets printed when I print inputed characters by their ASCII decimal code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int end;

    while(( end = getchar() ) != EOF ){
        printf("%d\n",end);
    }
    system("pause");
    return 0;
}

I want to print the ASCII codes of characters with this code but whenever I run the code after it gets the char from me it prints its ASCII equivalent with decimal 10. For example if I run the code and pass "a", it will print 97 and 10. Why does it print 10, this happens with all other characters too.我想用这个代码打印字符的 ASCII 代码,但是每当我在它从我这里获取字符后运行代码时,它都会用十进制 10 打印它的 ASCII 等效值。例如,如果我运行代码并传递“a”,它将打印97 和 10。为什么它打印 10,所有其他字符也会发生这种情况。 Thank you for answers and as a followup question when I add a counter after I input a character counter's value increases by two, why does this happen谢谢你的回答,作为一个后续问题,当我输入一个字符计数器的值增加 2 后添加一个计数器时,为什么会发生这种情况

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int end;
    int count=0;
    while(( end = getchar() ) != EOF ){
        printf("%d\n",end);
        count++;
        printf("counter is now %d\n",count);
    }
    system("pause");
    return 0;
}

As stated you are printing the ASCII decimal codes for 'a' and '\\n' respectively, this is because, in your code, getchar reads all the characters in the stdin buffer, including the newline character which is present because you press Enter .如上所述,您分别打印'a''\\n'的 ASCII 十进制代码,这是因为在您的代码中, getchar读取stdin缓冲区中的所有字符,包括由于您按下Enter而出现的换行符。

You can avoid this by simply making it be ignored by your condition:您可以通过简单地使其被您的条件忽略来避免这种情况:

while((end = getchar()) != EOF && end != '\n'){
    printf("%d\n", end);
}

Disclaimer: David Ranieri added a comment with the exact same solution as I was writing my answer (which he kindly deleted) so credit to him as well.免责声明:大卫·拉涅利 (David Ranieri) 添加了一条评论,其中的解决方案与我在写我的答案时完全相同(他善意地删除了),因此也归功于他。


Regarding your comment question and the question edit:关于您的评论问题和问题编辑:

If you don't want the '\\n' to interrupt your parsing cycle, you can simply place the condition inside it.如果您不希望'\\n'中断您的解析周期,您可以简单地将条件放在其中。

while((end = getchar()) != EOF){
    if(end != '\n'){ //now as '\n' is ignored, the counter increases by one
       printf("%d\n", end);
       count++; 
       printf("counter is now %d\n",count);
    }
}

The reason why the counter is increased by two is, again, because two characters are parsed, whatever character you input and the newline character.计数器增加 2 的原因再次是因为解析了两个字符,无论您输入什么字符和换行符。 As you can see in the sample, if you ignore the '\\n' , the counter will only increase by one, provided that you only enter one character at a time.正如您在示例中看到的,如果您忽略'\\n' ,则counter只会增加 1,前提是您一次只输入一个字符。

In ASCII, a newline is represented by the value 10.在 ASCII 中,换行符由值 10 表示。

Anytime you type a sequence of characters and press the ENTER key, you get the ASCII values for the letters/numbers/symbols you types as well as the value 10 for the newline.每当您键入一个字符序列并按 ENTER 键时,您都会得到您键入的字母/数字/符号的 ASCII 值以及换行符的值 10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM