0
 main()
{
char str[30];
int i ,n=0;
printf("enter stirng");
while(( str[n]= getche() ) !='\r')  
n++;
str[n]='\0';    
for(i=0;i<n;i++)
putchar(str[i]);
}

this is a simple program. enter string as input and same string become output.
nothing is wrong in this program. this program is in my book.
my question is what is the meaning of

while(( str[n]= getche() ) !='\r')

line i could not understood that. plz someone explain this line.

3
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by Daisy_1
0

str[n]= getche() will get a character from the keyboard and echos it to the screen. It then puts an integer into str on position(starting at zero) n. A check is done to see if that integer, returned by getche() is not equal with '\r'. If succes n is incremented and the while continues, else the while ends.
You also should test that n, never gets greater than 29.
You should write this code part like this:

while(( str[n]= getche() ) !='\r') 
{
  n++;
}
str[n]='\0';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.