void main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
}
o/p=11 why?

Answer Posted / suman halder

test cases:
1.
i/p:hi
o/p:11

2.
i/p:hello
o/p:11111

actually,getchar reads from stdin and is line buffered which means it will not return until you press ENTER.

so,look at the evaluation of the expression(c=getchar()!='\n')
here,
getchar()!='\n' evaluates to true until and unless u'll hit enter..that is why,the actual evaluation would look like
(c=(getchar()!='\n'))
so,
1 will be stored into c each time u press a key except enter key as (getchar()!='\n') evaluates to 1(true value of an expression).

finally,content of the buffer would get printed..
thats it..

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are 'near' and 'far' pointers?

611


Should a function contain a return statement if it does not return a value?

588


What are the basic data types associated with c?

805


what is the syallabus of computer science students in group- 1?

1832


What is the scope of local variable in c?

567






WRITE A CODE IN C TO SEARCH A FILE FROM NOTEPAD FILE.

2017


What is data structure in c and its types?

585


How can I remove the leading spaces from a string?

623


Differentiate between the = symbol and == symbol?

700


Write a program to swap two numbers without using a temporary variable?

601


What are compound statements?

622


a program that can input number of records and can view it again the record

1476


Here is a good puzzle: how do you write a program which produces its own source code as output?

591


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

652


What are the disadvantages of c language?

610