Well, you have a problem here. You use a function in your second sample code that is not part of the stdio package.
You call getch()
which is not a stdio function. It is part of the ncurses library, and, if you don't specify on compilation that you will use it, then you cannot get an executable program. So this make me thing you are not telling all the truth.
Just taking the function getch()
of of the program you get the full line
Priyanka
output, and the program terminated. I guess you used getch()
to stop the output until you press a character. But as curses library requires you to call initscr()
before calling any other curses library function, it is not correctly initialized, and the output you get can be wrong due to this.
I'll not repeat what others have already told you about the use of gets()
, it is still in the standard library, and knowing what you do, you can still use it in properly controlled environments. Despite of that, the recommendation others have given to you is not applicable here, as you have not overflowed the short buffer you have used (of only 34 chars, too short, too easy to hang your program or to crash it)
The functions from stdio use a buffer, and the unix tty driver is also interferring here. Your terminal will not make available any character you input to the program until you press the <ENTER>
key, then all those characters are read by the program into a buffer. They are consumed from the buffer, until it is empty, so it doesn't matter if you read them one by one (with fgetch()
, or all at once (with fgets()
---i'll use this, more secure, function, from this point on) Everything just happens once you press the <ENTER>
key.
fgetch()
only takes one character, so if more than one are available, only one character is taken from the buffer, and the rest wait their turn. But fgets()
reads all (and fills the buffer) until a \n
is read (this is why gets()
is so dangerous, because it doesn't know the size of your buffer /it doesn't have a parameter indicating the size of the buffer, as fgets()
has/ and cannot control the read to stop before overflowing it)
So, in your case, as you press a series of characters, then hit return, the first sample reads the full string, and then the second getchar()
takes the first of the second line (but you need to input two complete lines at that point) The second sample read the first char when you called getchar()
, and the rest of the line when you called gets()
.
To read one character at a time, without waiting for a full line to be input, the terminal driver has to be programmed to read characters in raw mode. Cookied mode (the default) is used by unix to read complete lines, this allows you to edit, erase characters on the line, and only input it when you are ready and hit the <ENTER>
key.
If you are interested in reading chars one by one from the terminal, read the manual page termios(4)
which explains the interface and iocontrols to the tty
device. The curses library does the necessary housekeeping to put the terminal in raw mode to allow programs like vi(1)
to read the input char by char, but you need then not to use stdio directly, as its buffering system will eat the characters you try to get to eat with curses.