Ashokkumar01cbe

Broken In
#include<stdio.h>
void main()
{
int c;
c=getchar();
while(c!='\n')
{
putchar(c);
c=getchar();
}
}

please explain me this program it is written in a c book written book of dennis ritchie, when i compile this programs,its getting a string of charecters and after pressing the ENTER button it is printing those string of charecters :confused:

please explain the above program ... run it in gcc compiler and explain
 
IIRC getchar takes single input of key from keyboard.So
initial assignment is c=getchar(),if you press enter then it wont pass the while loop condition otherwise it will enter the while loop
inside while loop the function putchar will print that character and waits to take the input from keyboard again.It is looping hence it ll continue printing untill u press enter
FYI C Programming | putchar
 
OP
Ashokkumar01cbe

Ashokkumar01cbe

Broken In
@manojkrishnaks, we all know that getchar() function will only get a character once but when we type the input as "DIGIT" as you said that it takes only the input D and get into the loop and the putchar() function needs to print all the character D and then only it needs to get the another character I, but here it takes all the input as "DIGIT" and after hitting the ENTER button it prints all the typed characters () DIGIT ... please explain me brother...:confused
:
 
OP
Ashokkumar01cbe

Ashokkumar01cbe

Broken In
manojkrishnaks you said that until pressing the enter character it will be in the loop, when i enter or input a sting of characters like" MANOJ "and it needs to take only the first character and it enter into the loop,and print the first character M but id does'nt do it, after hitting the ENTER button it displays all the characters...please explain me brother
 
manojkrishnaks you said that until pressing the enter character it will be in the loop, when i enter or input a sting of characters like" MANOJ "and it needs to take only the first character and it enter into the loop,and print the first character M but id does'nt do it, after hitting the ENTER button it displays all the characters...please explain me brother

Code:
#include<stdio.h>
void main()
{
int c;
clrscr();
c=getchar();
while(c!='\n')
{
getch();
putchar(c);
c=getchar();


}

}

see the behaviour of the program now!!!

some explanation below

*stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar

On a linux system, you can modify terminal behaviour using the stty command. By default, the terminal will buffer all information until ENTER is pressed, before even sending it to the C program.
*www.dragonwins.com/courses/ECE1021/STATIC/REFERENCES/getchar.htm
However, there is a side effect of this behavior – when the user types a key (say Y) and hits return, the call to getchar() returns ‘Y’ (the ASCII code for the character Y) as desired. However, there is still a character in the keyboard buffer – namely the ASCII code for the carriage return – and this is the value that will be returned the next time that getchar() is called. Therefore, we must have a means of clearing out the keyboard buffer either before we ask the user to enter something or as a cleanup task after we get their input
Hope It solves your doubt
 

karthikkumar

Broken In
IIRC getchar takes single input of key from keyboard.So
initial assignment is c=getchar(),if you press enter then it wont pass the while loop condition otherwise it will enter the while loop
inside while loop the function putchar will print that character and waits to take the input from keyboard again.It is looping hence it ll continue printing untill u press enter
FYI C Programming | putchar

Nice Way of Explaining :-D
 
Top Bottom