void main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
}
o/p=11 why?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / hari.11
above friend has posted correct answer,
11 is not the correct answer,
it will take all characters into buffer and will not output any answer until we press '\n' character.
So it would print 1 as many times as number of character pressed before '\n'.
e.g.:
s
o/p: 1
sd
o/p: 11
gdfdd
o/p: 11111
111
o/p: 111
for further queries and discussions, visit..
http://forum.campusmaniac.com/
http://www.campusmaniac.com/
| Is This Answer Correct ? | 2 Yes | 1 No |
Answer / sumeet saini
It says %d which means it prints integer value. If we want to print character we need to write %c. So the answer is right. If we replace %d with %c . This program will print what ever character we hit from keyboard until we press enter
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / prashant
The answer would have been 11 only if the while statement would have had a semicolon to finish with , i.e. "while(c=getchar()!='\n');" According to the above code it will generate the ascii of all the characters entered except the newline character.
Regards
Prashant
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / deepshree sinha
the while loop is continue until the expression is not false.
when the expression is false that is when c='\n' it will come
out from the loop.then it will print c which is equal to '\n'
whose integer value is 11.so it will print 11.
| Is This Answer Correct ? | 0 Yes | 1 No |
what does the following code do? fn(int n,int p,int r) { static int a=p; switch(n){ case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r; } } a.computes simple interest for one year b.computes amount on compound interest for 1 to 4 years c.computes simple interest for four year d.computes compound interst for 1 year
What is the incorrect operator form following list(== , <> , >= , <=) and what is the reason for the answer?
enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }
Are pointers integer?
#include<stdio.h> int f(int,int); int main() { printf("%d",f(20,1)); return 0; } int f(int n,int k) { if(n==0) return 0; else if(n%2)return f(n/2,2*k)+k; else return f(n/2,2*k)-k; } how this program is working and generating output as 9....?
write a program to concatenation the string using switch case?
What is variable initialization and why is it important?
define switch statement?
What are the commands should be given before weiting C Program i.e, Cd.. like
4 Answers IBM, Infonet, Satyam, Tech Mahindra,
Where in memory are my variables stored?
What is echo in c programming?
Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database
2 Answers TCS, Unisys, Webyog,