hey people, i wanted to ask,if i type a line and then press enter,how come getchar() is still able to read all the characters of a line..if it only reads a character after pressing enter....It appears as if its reading line by line ,not character by character....pl ease help me out with this confusion....
What does getchar return if it reads the "enter"...?
Collapse
X
-
-
Text streams such as stdin operate on a line by line basis with characters entered being placed in an input buffer until newline (or end of file) is entered. Then successive calls to getchar() read characters until the input buffer is exhausted (last character is newline or EOF).
Some C systems have functions which will read individual character by character directly from the keyboard, e.g. function getch() and getche() in <conio.h> in some Windows based compilers. Their use is not recommended as it is non standard and code is not protable.Comment
-
what if we suffix the input string with ctrl+z on a windows os(an EOF character).and then press enter....in that case getchar() should read eof as well and comeout of the loop no...Comment
-
Try the following fragment of code
Code:int main() { int ch=0; while(ch != EOF) { ch=getchar(); printf(" char %c %d",ch, ch); } } }
To enter EOF you enter CTRL/Z then RETURN key on a line by itself and it will break out of the loop. Using CodeBlock under Windows if I type characters then CTRL/Z then RETURN it passes the CTRL/Z as character code 26 decimal not EOF (which is -1 with this compiler)Comment
-
ok,that means with characters ctrl+z has ascii value 26 and when
provided alone getchar returns unsigned form of -1...(on this
compiler)...but enter is also there in the buffer then why does it not
print enter's ascii value which is 10.....Comment
-
the ASCII character code 10 decimal is displayed for RETURN key, e.g. if I type
abc<return>
abc<CTRL/Z><RETURN>
<CTRL/Z><RETURN>
I get
Code:char a 97 char b 98 char c 99 char 10 char a 97 char b 98 char c 99 char 26 char ΓΏ -1
Comment
Comment