You should get a table of ascii characters and look up the character equivalent for 10.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
>(1) "(when we insert one character)Why code prints "10"
>after printing ascii value of the character we insert?"
Because you actually typed two characters? You press the 'a' key right? Then you press then [Enter] key, right? That's two characters: 'a' and newline.
I'd love to hear why you think you only "inserted" N-1 characters when you clearly pressed N keys on your keyboard.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
So? You leave the \n in the buffer and exit? What does that prove? Try typing "abcdefgh". What happens?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Read very carefully. Turn off the TV, turn off the radio. Study this post.
When you enter anything into an input, each and every key you press (except the obvious CTRL, ALT, etc) is a character. Including the ENTER/RETURN key! You type in a and hit ENTER, you have 2 (two, dos, deux...) characters in the buffer.
You now use getchar() . How many characters does it read? 1 (one, uno, un). You output the character read, you get97. That leaves the ENTER in the buffer.
If you exit, you leave that ENTER in the buffer. You loop, the getchar() reads the ENTER. You output the character read, you get10
Any more questions? If so, you'll just have to believe us.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>I have heard that C is a cryptic language, and
>some parts of it (like this one!) make me feel it true.
There are parts of C that can be confusing, but this isn't one of them. getchar reads one character from the stream. Let's say the stream contains "abcdef\n". If you call getchar once, you'll get 'a'. Call it again and you'll get 'b'. Keep calling it and eventually you'll get '\n'.
'\n' is just another character that gets pushed onto the stream when you press the [Enter] key. Your results from the test programs are exactly what I'd expect.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Try this version of your code:
#include<stdio.h>
int main()
{
int i=getchar();
while(i!=9)
{
if (i >= 32 && i < 127)
{
printf("The character '%c' (%d) was entered\n",i,i);
}
else
if (i == 10)
{
printf("The ENTER key (%d) was pressed \n",i);
}
else
{
printf("A weird character was pressed (%d)\n",i);
}
i=getchar();
}
printf("%d",i);
return 0;
}
Does this help?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
The key to this situation is that even when you are not using getchar within a loop the extra '\n' is still in the input stream.
Since you are not accessing the stream again it just becomes a distant "memory" as your program finishes.
Compare that to when you are going through the loop for the nth time the '\n' character is still hanging around and gets read in.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
What I had also meant to say (I ran out of edit time) was the the others have explained it very well but this is just my 0.02.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581