PDA

View Full Version : [SOLVED] Taking Keyboard return as input



c2tarun
February 5th, 2012, 04:34 AM
Hi friends
I tried following code in gcc compiler.



#include <stdio.h>

int main()
{
char a;
int i = 0;
while(i<10){
a=getchar();
printf("%c,%d\n",a,(int)a);
}
}


I am getting following as output


$ ./checkgetchar < getcharInput
T,84
a,97
r,114
u,117
n,110
K,75
u,117
m,109

,10
a,97


The content of input file getcharInput is

TarunKum
ar123

Can anyone please explain me why is it taking two inputs after m and why am I not getting the ASCII code of return. :(

c2tarun
February 5th, 2012, 04:39 AM
I just understood it :)
I think it is printing the return and that is why there is a line change and it is not two inputs. Please correct me if i am wrong.

Arndt
February 5th, 2012, 09:51 AM
I just understood it :)
I think it is printing the return and that is why there is a line change and it is not two inputs. Please correct me if i am wrong.

You found the answer. But you were correct that you're not getting the ASCII code 13 for return, but 10 for linefeed. That's because the terminal driver (and/or terminal emulator) translates the return key that you press into linefeed, since linefeed is the standard end-of-line character in Unix.