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

Explain the advantages of using macro in c language?

584


Are pointers integer?

554


Is there sort function in c?

581


What are the 4 types of unions?

614


What is the difference between call by value and call by reference in c?

621






Write a program to print fibonacci series using recursion?

591


write a program to find the given number is prime or not

3849


What is the significance of c program algorithms?

685


Explain the use of bit fieild.

717


What are the different types of linkage exist in c?

615


What are the different types of constants?

644


Is c programming hard?

578


What are # preprocessor operator in c?

634


find the output? void r(int a[],int c, int n) { if(c>n) { a[c]=a[c]+c; r(a,++c,n); r(a,++c,n); } } int main() { int i,a[5]={0}; r(a,0,5); for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }

1863


Define and explain about ! Operator?

618