Unfortunately, I'm not sure if I understood your question correctly.
However, under Linux the program would work the following way:
You close your eyes, assume your program works correctly and type. You will see:
put your pass key: KMartin
Enter your name: Enter your address: Hello
--------------------------
Entered Name: Martin
Entered Address: Hello
Your pass key: K
Why?
When you type some keys in Linux, the operating system will store the keys pressed in some buffer until the "enter" ("return") key is pressed.
Maybe MINGW has the same behaviour.
So the program reaches getchar()
and I type "K" "M" "a" "r" "t" "i" "n".
Linux stores these keys into a buffer and instead of passing these keys to the program.
I press the "enter" ("return") key. Then Linux will pass the "K" to the getchar()
function and the remaining keys ("Martin") to the scanf()
function.
This behaviour can be disabled:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
int main(void) {
char name[20], address[30];
char c;
struct termios tios;
printf("put your pass key: ");
/* Disable the buffer */
tcgetattr(1,&tios);
tios.c_lflag&=~ICANON;
tios.c_cc[VTIME]=0;
tios.c_cc[VMIN]=1;
tcsetattr(1,TCSANOW,&tios);
c = getchar();
/* Re-enable the buffer
* If the buffer is not enabled,
* sscanf() won't work correctly. */
tios.c_lflag|=ICANON;
tcsetattr(1,TCSANOW,&tios);
printf("\nEnter your name: ");
...
... and the output will be:
put your pass key: K
Enter your name: Martin
Enter your address: Hello
--------------------------
Entered Name: Martin
Entered Address: Hello
Your pass key: K
EDIT
i run it on eclipse and it's shows nothing but when using repl like @user366312 it works I don't see anything in console on eclipse
The consoles in Eclipse typically do not allow input so programs can write output but not receive input.
However, I know that there is some check box (at least in some Eclipse versions) that allow input...
I don't know if you will have the problem I described above when using Eclipse.
However, if you have that behaviour, the settermios
method probably won't work...